home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / jikes-1.02 / src / expr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-26  |  303.4 KB  |  7,181 lines

  1. // $Id: expr.cpp,v 1.29 1999/08/26 15:34:07 shields Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include "double.h"
  11. #include "config.h"
  12. #include <assert.h>
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <sys/stat.h>
  16. #include "parser.h"
  17. #include "semantic.h"
  18. #include "control.h"
  19. #include "table.h"
  20. #include "tuple.h"
  21. #include "spell.h"
  22.  
  23. bool Semantic::IsIntValueRepresentableInType(AstExpression *expr, TypeSymbol *type)
  24. {
  25.     IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  26.  
  27.     return (expr -> IsConstant() &&
  28.             //
  29.             // TODO: the test:
  30.             //
  31.             //    control.IsSimpleIntegerValueType(expr -> type())) &&
  32.             //
  33.             // makes more sense than the test:
  34.             //
  35.             //    expr -> Type() == control.int_type) &&
  36.             //
  37.             // below which is specified in the JLS.
  38.             //
  39.             expr -> Type() == control.int_type) &&
  40.             (type == control.int_type ||
  41.              type == control.no_type  ||
  42.              (type == control.char_type && (literal -> value >= 0)  && (literal -> value <= 65535)) ||
  43.              (type == control.byte_type && (literal -> value >= -128) && (literal -> value <= 127)) ||
  44.              (type == control.short_type && (literal -> value >= -32768)  && (literal -> value <= 32767)));
  45. }
  46.  
  47.  
  48. inline bool Semantic::MoreSpecific(MethodSymbol *source_method, MethodSymbol *target_method)
  49. {
  50.     if (! CanMethodInvocationConvert(target_method -> containing_type, source_method -> containing_type))
  51.         return false;
  52.  
  53.     for (int k = target_method -> NumFormalParameters() - 1; k >= 0; k--)
  54.     {
  55.         if (! CanMethodInvocationConvert(target_method -> FormalParameter(k) -> Type(),
  56.                                          source_method -> FormalParameter(k) -> Type()))
  57.             return false;
  58.     }
  59.  
  60.     return true;
  61. }
  62.  
  63.  
  64. inline bool Semantic::MoreSpecific(MethodSymbol *method, Tuple<MethodSymbol *> &maximally_specific_method)
  65. {
  66.     for (int i = 0; i < maximally_specific_method.Length(); i++)
  67.     {
  68.         if (! MoreSpecific(method, maximally_specific_method[i]))
  69.             return false;
  70.     }
  71.  
  72.     return true;
  73. }
  74.  
  75.  
  76. inline bool Semantic::NoMethodMoreSpecific(Tuple<MethodSymbol *> &maximally_specific_method, MethodSymbol *method)
  77. {
  78.     for (int i = 0; i < maximally_specific_method.Length(); i++)
  79.     {
  80.         if (MoreSpecific(maximally_specific_method[i], method))
  81.             return false;
  82.     }
  83.  
  84.     return true;
  85. }
  86.  
  87.  
  88. void Semantic::ReportMethodNotFound(Ast *ast, wchar_t *name)
  89. {
  90.     SemanticError::SemanticErrorKind kind;
  91.  
  92.     int num_arguments;
  93.     AstExpression **argument;
  94.  
  95.     AstMethodInvocation *method_call;
  96.     AstClassInstanceCreationExpression *class_creation;
  97.     AstThisCall *this_call;
  98.     AstSuperCall *super_call;
  99.     if (method_call = ast -> MethodInvocationCast())
  100.     {
  101.         kind = SemanticError::METHOD_NOT_FOUND;
  102.         num_arguments = method_call -> NumArguments();
  103.         argument = new AstExpression*[num_arguments + 1];
  104.         for (int i = 0; i < num_arguments; i++)
  105.             argument[i] = method_call -> Argument(i);
  106.     }
  107.     else
  108.     {
  109.         kind = SemanticError::CONSTRUCTOR_NOT_FOUND;
  110.  
  111.         if (class_creation = ast -> ClassInstanceCreationExpressionCast())
  112.         {
  113.             num_arguments = class_creation -> NumArguments();
  114.             argument = new AstExpression*[num_arguments + 1];
  115.             for (int i = 0; i < num_arguments; i++)
  116.                 argument[i] = class_creation -> Argument(i);
  117.         }
  118.         else if (super_call = ast -> SuperCallCast())
  119.         {
  120.             num_arguments = super_call -> NumArguments();
  121.             argument = new AstExpression*[num_arguments + 1];
  122.             for (int i = 0; i < num_arguments; i++)
  123.                 argument[i] = super_call -> Argument(i);
  124.         }
  125.         else
  126.         {
  127.             this_call = ast -> ThisCallCast();
  128.  
  129.             assert(this_call);
  130.  
  131.             num_arguments = this_call -> NumArguments();
  132.             argument = new AstExpression*[num_arguments + 1];
  133.             for (int i = 0; i < num_arguments; i++)
  134.                 argument[i] = this_call -> Argument(i);
  135.         }
  136.     }
  137.  
  138.     int length = wcslen(name);
  139.  
  140.     for (int i = 0; i < num_arguments; i++)
  141.     {
  142.         TypeSymbol *arg_type = argument[i] -> Type();
  143.         length += arg_type -> ContainingPackage() -> PackageNameLength() +
  144.                   arg_type -> ExternalNameLength() + 3; // '/' after package_name
  145.                                                         // ',' and ' ' to separate this formal parameter from the next one
  146.     }
  147.  
  148.     wchar_t *header = new wchar_t[length + 3]; // +1 for (, +1 for ), +1 for '\0'
  149.     wchar_t *s = header;
  150.  
  151.     for (wchar_t *s2 = name; *s2; s2++)
  152.          *s++ = *s2;
  153.     *s++ = U_LEFT_PARENTHESIS;
  154.     if (num_arguments > 0)
  155.     {
  156.         for (int i = 0; i < num_arguments; i++)
  157.         {
  158.             TypeSymbol *arg_type = argument[i] -> Type();
  159.  
  160.             PackageSymbol *package = arg_type -> ContainingPackage();
  161.             wchar_t *package_name = package -> PackageName();
  162.             if (package -> PackageNameLength() > 0 && wcscmp(package_name, StringConstant::US__DO) != 0)
  163.             {
  164.                 while (*package_name)
  165.                 {
  166.                     *s++ = (*package_name == U_SLASH ? (wchar_t) U_DOT : *package_name);
  167.                     package_name++;
  168.                 }
  169.                 *s++ = U_DOT;
  170.             }
  171.  
  172.             for (wchar_t *s2 = arg_type -> ExternalName(); *s2; s2++)
  173.                 *s++ = (*s2 == U_DOLLAR ? (wchar_t) U_DOT : *s2);
  174.             *s++ = U_COMMA;
  175.             *s++ = U_SPACE;
  176.         }
  177.  
  178.         s -= 2; // remove the last ',' and ' '
  179.     }
  180.     *s++ = U_RIGHT_PARENTHESIS;
  181.     *s = U_NULL;
  182.  
  183.     ReportSemError(kind,
  184.                    ast -> LeftToken(),
  185.                    ast -> RightToken(),
  186.                    header);
  187.  
  188.     delete [] header;
  189.     delete [] argument;
  190.  
  191.     return;
  192. }
  193.  
  194.  
  195. MethodSymbol *Semantic::FindConstructor(TypeSymbol *containing_type, Ast *ast,
  196.                                         LexStream::TokenIndex left_tok, LexStream::TokenIndex right_tok)
  197. {
  198.     Tuple<MethodSymbol *> constructor_set(2);
  199.  
  200.     int num_arguments;
  201.     AstExpression **argument;
  202.  
  203.     AstClassInstanceCreationExpression *class_creation;
  204.     AstThisCall *this_call;
  205.     AstSuperCall *super_call;
  206.     if (class_creation = ast -> ClassInstanceCreationExpressionCast())
  207.     {
  208.         num_arguments = class_creation -> NumArguments();
  209.         argument = new AstExpression*[num_arguments + 1];
  210.         for (int i = 0; i < num_arguments; i++)
  211.             argument[i] = class_creation -> Argument(i);
  212.     }
  213.     else if (super_call = ast -> SuperCallCast())
  214.     {
  215.         num_arguments = super_call -> NumArguments();
  216.         argument = new AstExpression*[num_arguments + 1];
  217.         for (int i = 0; i < num_arguments; i++)
  218.             argument[i] = super_call -> Argument(i);
  219.     }
  220.     else
  221.     {
  222.         this_call = ast -> ThisCallCast();
  223.  
  224.         assert(this_call);
  225.  
  226.         num_arguments = this_call -> NumArguments();
  227.         argument = new AstExpression*[num_arguments + 1];
  228.         for (int i = 0; i < num_arguments; i++)
  229.             argument[i] = this_call -> Argument(i);
  230.     }
  231.  
  232.     assert(containing_type -> ConstructorMembersProcessed());
  233.  
  234.     for (MethodSymbol *constructor = containing_type -> FindConstructorSymbol();
  235.          constructor; constructor = constructor -> next_method)
  236.     {
  237.         if (! constructor -> IsTyped())
  238.             constructor -> ProcessMethodSignature((Semantic *) this, right_tok);
  239.  
  240.         if (num_arguments == constructor -> NumFormalParameters())
  241.         {
  242.             int i;
  243.             for (i = 0; i < num_arguments; i++)
  244.             {
  245.                 if (! CanMethodInvocationConvert(constructor -> FormalParameter(i) -> Type(), argument[i] -> Type()))
  246.                     break;
  247.             }
  248.             if (i == num_arguments)
  249.             {
  250.                 if (MoreSpecific(constructor, constructor_set))
  251.                 {
  252.                     constructor_set.Reset();
  253.                     constructor_set.Next() = constructor;
  254.                 }
  255.                 else if (NoMethodMoreSpecific(constructor_set, constructor))
  256.                     constructor_set.Next() = constructor;
  257.             }
  258.         }
  259.     }
  260.  
  261.     if (constructor_set.Length() == 0)
  262.     {
  263.         MethodSymbol *method;
  264.         for (method = containing_type -> FindMethodSymbol(containing_type -> Identity()); method; method = method -> next_method)
  265.         {
  266.             if (! method -> IsTyped())
  267.                 method -> ProcessMethodSignature((Semantic *) this, right_tok);
  268.  
  269.             if (num_arguments == method -> NumFormalParameters())
  270.             {
  271.                 int i;
  272.                 for (i = 0; i < num_arguments; i++)
  273.                 {
  274.                     if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), argument[i] -> Type()))
  275.                         break;
  276.                 }
  277.                 if (i == num_arguments)
  278.                     break;
  279.             }
  280.         }
  281.  
  282.         if (method)
  283.         {
  284.             if (method -> method_or_constructor_declaration)
  285.             {
  286.                 AstMethodDeclaration *method_declaration = (AstMethodDeclaration *) method -> method_or_constructor_declaration;
  287.                 FileLocation loc(method -> containing_type -> semantic_environment -> sem -> lex_stream,
  288.                                  method_declaration -> method_declarator -> identifier_token);
  289.  
  290.                 ReportSemError(SemanticError::METHOD_FOUND_FOR_CONSTRUCTOR,
  291.                                left_tok,
  292.                                right_tok,
  293.                                containing_type -> Name(),
  294.                                loc.location);
  295.             }
  296.             else
  297.             {
  298.                 ReportSemError(SemanticError::METHOD_FOUND_FOR_CONSTRUCTOR,
  299.                                left_tok,
  300.                                right_tok,
  301.                                containing_type -> Name(),
  302.                                method -> containing_type -> file_location -> location);
  303.             }
  304.         }
  305.         else ReportMethodNotFound(ast, containing_type -> Name());
  306.  
  307.         delete [] argument;
  308.  
  309.         return (MethodSymbol *) NULL;
  310.     }
  311.     else if (constructor_set.Length() > 1)
  312.     {
  313.         ReportSemError(SemanticError::AMBIGUOUS_CONSTRUCTOR_INVOCATION,
  314.                        left_tok,
  315.                        right_tok,
  316.                        containing_type -> Name());
  317.     }
  318.  
  319.     delete [] argument;
  320.  
  321.     MethodSymbol *constructor_symbol = constructor_set[0];
  322.  
  323.     if (constructor_symbol -> IsSynthetic())
  324.     {
  325.         ReportSemError(SemanticError::SYNTHETIC_CONSTRUCTOR_INVOCATION,
  326.                        left_tok,
  327.                        right_tok,
  328.                        constructor_symbol -> Header(),
  329.                        containing_type -> ContainingPackage() -> PackageName(),
  330.                        containing_type -> ExternalName());
  331.     }
  332.  
  333.     //
  334.     // If this constructor came from a class file, make sure that its throws clause has been processed.
  335.     //
  336.     constructor_symbol -> ProcessMethodThrows((Semantic *) this, right_tok);
  337.  
  338.     if (control.option.deprecation &&
  339.         constructor_symbol -> IsDeprecated() &&
  340.         constructor_symbol -> containing_type -> outermost_type != ThisType() -> outermost_type)
  341.     {
  342.         ReportSemError(SemanticError::DEPRECATED_METHOD,
  343.                        left_tok,
  344.                        right_tok,
  345.                        constructor_symbol -> Header(),
  346.                        constructor_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  347.                        constructor_symbol -> containing_type -> ExternalName());
  348.     }
  349.  
  350.     return constructor_symbol;
  351. }
  352.  
  353.  
  354. //
  355. //
  356. //
  357. VariableSymbol *Semantic::FindMisspelledVariableName(TypeSymbol *type, LexStream::TokenIndex identifier_token)
  358. {
  359.     VariableSymbol *misspelled_variable = NULL;
  360.     int index = 0;
  361.     wchar_t *name = lex_stream -> NameString(identifier_token);
  362.  
  363.     for (int k = 0; k < type -> expanded_field_table -> symbol_pool.Length(); k++)
  364.     {
  365.         VariableShadowSymbol *variable_shadow = type -> expanded_field_table -> symbol_pool[k];
  366.         VariableSymbol *variable = variable_shadow -> variable_symbol;
  367.         if (! variable -> IsTyped())
  368.             variable -> ProcessVariableSignature((Semantic *) this, identifier_token);
  369.  
  370.         int new_index = Spell::Index(name, variable -> Name());
  371.         if (new_index > index)
  372.         {
  373.             misspelled_variable = variable;
  374.             index = new_index;
  375.         }
  376.     }
  377.  
  378.     int length = wcslen(name);
  379.  
  380.     return ((length == 3 && index >= 5) || (length == 4 && index >= 6) || (length >= 5 && index >= 7)
  381.                           ? misspelled_variable
  382.                           : (VariableSymbol *) NULL);
  383. }
  384.  
  385. //
  386. //
  387. //
  388. MethodSymbol *Semantic::FindMisspelledMethodName(TypeSymbol *type, AstMethodInvocation *method_call, NameSymbol *name_symbol)
  389. {
  390.     MethodSymbol *misspelled_method = NULL;
  391.     int index = 0;
  392.     AstSimpleName *simple_name = method_call -> method -> SimpleNameCast();
  393.     AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  394.     LexStream::TokenIndex identifier_token = (simple_name ? simple_name -> identifier_token : field_access -> identifier_token);
  395.  
  396.     for (int k = 0; k < type -> expanded_method_table -> symbol_pool.Length(); k++)
  397.     {
  398.         MethodShadowSymbol *method_shadow = type -> expanded_method_table -> symbol_pool[k];
  399.         MethodSymbol *method = method_shadow -> method_symbol;
  400.         TypeSymbol *containing_type = method -> containing_type;
  401.  
  402.         if (! method -> IsTyped())
  403.             method -> ProcessMethodSignature((Semantic *) this, identifier_token);
  404.  
  405.         if (method_call -> NumArguments() == method -> NumFormalParameters())
  406.         {
  407.             int i;
  408.             for (i = 0; i < method_call -> NumArguments(); i++)
  409.             {
  410.                 AstExpression *expr = method_call -> Argument(i);
  411.                 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  412.                     break;
  413.             }
  414.             if (i == method_call -> NumArguments())
  415.             {
  416.                 int new_index = Spell::Index(name_symbol -> Name(), method -> Name());
  417.                 if (new_index > index)
  418.                 {
  419.                     misspelled_method = method;
  420.                     index = new_index;
  421.                 }
  422.             }
  423.         }
  424.     }
  425.  
  426.     int length = name_symbol -> NameLength(),
  427.          num_args = method_call -> NumArguments();
  428.  
  429.     //
  430.     // If we have a name of length 2, accept >= 30% probality if the function takes at least one argument
  431.     // If we have a name of length 3, accept >= 50% probality if the function takes at least one argument
  432.     // Otherwise, if the length of the name is > 3, accept >= 60% probability.
  433.     //
  434.     return (index < 3 ? (MethodSymbol *) NULL
  435.                       : (length == 2 && (index >= 3 || num_args > 0)) ||
  436.                         (length == 3 && (index >= 5 || num_args > 0)) ||
  437.                         (length  > 3 && (index >= 6 || (index >= 5 && num_args > 0)))
  438.                                      ? misspelled_method
  439.                                      : (MethodSymbol *) NULL);
  440. }
  441.  
  442.  
  443. //
  444. // This method is a mirror image of MemberAccessCheck.
  445. //
  446. bool Semantic::IsMethodAccessible(AstFieldAccess *field_access, TypeSymbol *base_type, MethodSymbol *method_symbol)
  447. {
  448.     TypeSymbol *this_type = ThisType(),
  449.                *containing_type = method_symbol -> containing_type;
  450.  
  451.     return (this_type -> outermost_type == containing_type -> outermost_type) ||
  452.             method_symbol -> ACC_PUBLIC() ||
  453.             ((! method_symbol -> ACC_PRIVATE()) && containing_type -> ContainingPackage() == this_package) ||
  454.             (method_symbol -> ACC_PROTECTED() && (field_access -> base -> IsSuperExpression() ||
  455.                                                   (this_type -> HasProtectedAccessTo(containing_type) &&
  456.                                                    (base_type -> IsSubclass(this_type) || base_type -> IsOwner(this_type)))));
  457. }
  458.  
  459.  
  460. //
  461. // Search the type in question for a method. Note that name_symbol is an optional argument.
  462. // If it was not passed to this function then its default value is NULL (see semantic.h) and
  463. // we assume that the name to search for is the name specified in the field_access of the
  464. // method_call.
  465. //
  466. MethodSymbol *Semantic::FindMethodInType(TypeSymbol *type, AstMethodInvocation *method_call, NameSymbol *name_symbol)
  467. {
  468.     Tuple<MethodSymbol *> method_set(2);
  469.     AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  470.     if (! name_symbol)
  471.         name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  472.  
  473.     if (! type -> expanded_method_table)
  474.         ComputeMethodsClosure(type, field_access -> identifier_token);
  475.  
  476. //
  477. // TODO: Confirm that this is no longer the case as of javac 1.2
  478. //
  479. /*
  480.     //
  481.     // First look for the method in the "type". If it is not found, look for
  482.     // it in the superclasses in the proper order. It is possible that the
  483.     // method in question is a private field that is contained in the body
  484.     // of the type that we are currently processing (this_type()), in which
  485.     // case it is accessible even though it is not directly inherited by "type".
  486.     //
  487.     for (TypeSymbol *type_symbol = type; type_symbol && method_set.Length() == 0; type_symbol = type_symbol -> super)
  488.     {
  489. */
  490.         TypeSymbol *type_symbol = type;
  491.  
  492.         for (MethodShadowSymbol *method_shadow = type_symbol -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  493.              method_shadow; method_shadow = method_shadow -> next_method)
  494.         {
  495.             MethodSymbol *method = method_shadow -> method_symbol;
  496.             TypeSymbol *containing_type = method -> containing_type;
  497.  
  498.             if (! method -> IsTyped())
  499.                 method -> ProcessMethodSignature((Semantic *) this, field_access -> identifier_token);
  500.  
  501.             if (method_call -> NumArguments() == method -> NumFormalParameters() &&
  502.                 IsMethodAccessible(field_access, type_symbol, method))
  503.             {
  504.                 int i;
  505.                 for (i = 0; i < method_call -> NumArguments(); i++)
  506.                 {
  507.                     AstExpression *expr = method_call -> Argument(i);
  508.                     if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  509.                         break;
  510.                 }
  511.                 if (i == method_call -> NumArguments())
  512.                 {
  513.                     if (MoreSpecific(method, method_set))
  514.                     {
  515.                         method_set.Reset();
  516.                         method_set.Next() = method;
  517.                     }
  518.                     else if (NoMethodMoreSpecific(method_set, method))
  519.                         method_set.Next() = method;
  520.                 }
  521.             }
  522.         }
  523.  
  524. /*
  525. See comment above...
  526.     }
  527. */
  528.  
  529.     if (method_set.Length() == 0)
  530.     {
  531.         if (! type -> expanded_field_table)
  532.             ComputeFieldsClosure(type, field_access -> identifier_token);
  533.  
  534.         VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  535.  
  536.         if (variable_shadow_symbol)
  537.         {
  538.             VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  539.             TypeSymbol *enclosing_type = variable_symbol -> owner -> TypeCast();
  540.  
  541.             assert(enclosing_type);
  542.  
  543.             ReportSemError(SemanticError::FIELD_NOT_METHOD,
  544.                            method_call -> LeftToken(),
  545.                            method_call -> RightToken(),
  546.                            variable_symbol -> Name(),
  547.                            enclosing_type -> ContainingPackage() -> PackageName(),
  548.                            enclosing_type -> ExternalName());
  549.         }
  550.         else
  551.         {
  552.             TypeSymbol *super_type;
  553.             MethodShadowSymbol *method_shadow;
  554.  
  555.             //
  556.             // Check whether or not the method we are looking fr is not an inaccessible private method.
  557.             //
  558.             for (super_type = type; super_type; super_type = super_type -> super)
  559.             {
  560.                 for (method_shadow = super_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  561.                      method_shadow; method_shadow = method_shadow -> next_method)
  562.                 {
  563.                     MethodSymbol *method = method_shadow -> method_symbol;
  564.                     if (! method -> IsTyped())
  565.                         method -> ProcessMethodSignature((Semantic *) this, field_access -> identifier_token);
  566.  
  567.                     if (method_call -> NumArguments() == method -> NumFormalParameters())
  568.                     {
  569.                         int i;
  570.                         for (i = 0; i < method_call -> NumArguments(); i++)
  571.                         {
  572.                             AstExpression *expr = method_call -> Argument(i);
  573.                             if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  574.                                 break;
  575.                         }
  576.                         if (i == method_call -> NumArguments()) // found a match ?
  577.                             break;
  578.                     }
  579.                 }
  580.  
  581.                 if (method_shadow) // found a match ?
  582.                     break;
  583.             }
  584.  
  585.             if (super_type)
  586.             {
  587.                 ReportSemError((method_shadow -> method_symbol -> ACC_PRIVATE()
  588.                                                ? SemanticError::METHOD_WITH_PRIVATE_ACCESS_NOT_ACCESSIBLE
  589.                                                : SemanticError::METHOD_WITH_DEFAULT_ACCESS_NOT_ACCESSIBLE),
  590.                                method_call -> LeftToken(),
  591.                                method_call -> RightToken(),
  592.                                method_shadow -> method_symbol -> Header(),
  593.                                super_type -> ContainingPackage() -> PackageName(),
  594.                                super_type -> ExternalName(),
  595.                                ThisType() -> ContainingPackage() -> PackageName(),
  596.                                ThisType() -> ExternalName());
  597.             }
  598.             else
  599.             {
  600.                 if (FindNestedType(type, field_access -> identifier_token))
  601.                 {
  602.                     ReportSemError(SemanticError::TYPE_NOT_METHOD,
  603.                                    field_access -> identifier_token,
  604.                                    field_access -> identifier_token,
  605.                                    name_symbol -> Name());
  606.                 }
  607.                 else if (type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol))
  608.                     ReportMethodNotFound(method_call, name_symbol -> Name());
  609.                 else
  610.                 {
  611.                     MethodSymbol *method = FindMisspelledMethodName(type, method_call, name_symbol);
  612.                     if (method)
  613.                          ReportSemError(SemanticError::METHOD_NAME_MISSPELLED,
  614.                                         method_call -> LeftToken(),
  615.                                         method_call -> RightToken(),
  616.                                         name_symbol -> Name(),
  617.                                         type -> ContainingPackage() -> PackageName(),
  618.                                         type -> ExternalName(),
  619.                                         method -> Name());
  620.                     else ReportSemError(SemanticError::METHOD_NAME_NOT_FOUND_IN_TYPE,
  621.                                         method_call -> LeftToken(),
  622.                                         method_call -> RightToken(),
  623.                                         name_symbol -> Name(),
  624.                                         type -> ContainingPackage() -> PackageName(),
  625.                                         type -> ExternalName());
  626.                 }
  627.             }
  628.         }
  629.  
  630.         return (MethodSymbol *) NULL;
  631.     }
  632.     else if (method_set.Length() > 1)
  633.     {
  634.         ReportSemError(SemanticError::AMBIGUOUS_METHOD_INVOCATION,
  635.                        method_call -> LeftToken(),
  636.                        method_call -> RightToken(),
  637.                        name_symbol -> Name(),
  638.                        method_set[0] -> Header(),
  639.                        method_set[0] -> containing_type -> ContainingPackage() -> PackageName(),
  640.                        method_set[0] -> containing_type -> ExternalName(),
  641.                        method_set[1] -> Header(),
  642.                        method_set[1] -> containing_type -> ContainingPackage() -> PackageName(),
  643.                        method_set[1] -> containing_type -> ExternalName());
  644.     }
  645.  
  646.     MethodSymbol *method = method_set[0];
  647.     if (method -> IsSynthetic())
  648.     {
  649.         ReportSemError(SemanticError::SYNTHETIC_METHOD_INVOCATION,
  650.                        method_call -> LeftToken(),
  651.                        method_call -> RightToken(),
  652.                        method -> Header(),
  653.                        method -> containing_type -> ContainingPackage() -> PackageName(),
  654.                        method -> containing_type -> ExternalName());
  655.     }
  656.  
  657.     //
  658.     // If this method came from a class file, make sure that its throws clause has been processed.
  659.     //
  660.     method -> ProcessMethodThrows((Semantic *) this, field_access -> identifier_token);
  661.  
  662.     if (control.option.deprecation &&
  663.         method -> IsDeprecated() && method -> containing_type -> outermost_type != ThisType() -> outermost_type)
  664.     {
  665.         ReportSemError(SemanticError::DEPRECATED_METHOD,
  666.                        method_call -> LeftToken(),
  667.                        method_call -> RightToken(),
  668.                        method -> Header(),
  669.                        method -> containing_type -> ContainingPackage() -> PackageName(),
  670.                        method -> containing_type -> ExternalName());
  671.     }
  672.  
  673.     return method;
  674. }
  675.  
  676.  
  677. void Semantic::SearchForMethodInEnvironment(Tuple<MethodSymbol *> &methods_found,
  678.                                             SemanticEnvironment *&where_found,
  679.                                             SemanticEnvironment *stack,
  680.                                             AstMethodInvocation *method_call)
  681. {
  682.     AstSimpleName *simple_name = method_call -> method -> SimpleNameCast();
  683.     NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  684.  
  685.     for (SemanticEnvironment *env = stack; env; env = env -> previous)
  686.     {
  687.         TypeSymbol *type = env -> Type();
  688.         if (! type -> expanded_method_table)
  689.             ComputeMethodsClosure(type, simple_name -> identifier_token);
  690.  
  691.         methods_found.Reset();
  692.         where_found = NULL;
  693.  
  694.         //
  695.         // If this environment contained a method with the right name, the search stops:
  696.         //
  697.         //    "Class scoping does not influence overloading: if the inner class has one
  698.         //     print method, the simple method name 'print' refers to that method, not
  699.         //     any of the ten 'print' methods in the enclosing class."
  700.         //
  701.         MethodShadowSymbol *method_shadow = type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  702.         if (method_shadow)
  703.         {
  704.             for (; method_shadow; method_shadow = method_shadow -> next_method)
  705.             {
  706.                 MethodSymbol *method = method_shadow -> method_symbol;
  707.                 TypeSymbol *containing_type = method -> containing_type;
  708.  
  709.                 if (! method -> IsTyped())
  710.                     method -> ProcessMethodSignature((Semantic *) this, simple_name -> identifier_token);
  711.  
  712.                 //
  713.                 // Since type -> IsOwner(this_type()), i.e., type encloses this_type(),
  714.                 // method is accessible, even if it is private.
  715.                 //
  716.                 if (method_call -> NumArguments() == method -> NumFormalParameters())
  717.                 {
  718.                     int i;
  719.                     for (i = 0; i < method_call -> NumArguments(); i++)
  720.                     {
  721.                         AstExpression *expr = method_call -> Argument(i);
  722.                         if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  723.                             break;
  724.                     }
  725.                     if (i == method_call -> NumArguments())
  726.                     {
  727.                         if (MoreSpecific(method, methods_found))
  728.                         {
  729.                             methods_found.Reset();
  730.                             methods_found.Next() = method;
  731.                         }
  732.                         else if (NoMethodMoreSpecific(methods_found, method))
  733.                             methods_found.Next() = method;
  734.                     }
  735.                 }
  736.             }
  737.  
  738.             //
  739.             // If a match was found, save the environment
  740.             //
  741.             where_found = (methods_found.Length() > 0 ? env : (SemanticEnvironment *) NULL);
  742.             break;
  743.         }
  744.     }
  745.  
  746.     return;
  747. }
  748.  
  749.  
  750. MethodSymbol *Semantic::FindMethodInEnvironment(SemanticEnvironment *&where_found,
  751.                                                 SemanticEnvironment *stack,
  752.                                                 AstMethodInvocation *method_call)
  753. {
  754.     Tuple<MethodSymbol *> methods_found(2);
  755.     SearchForMethodInEnvironment(methods_found, where_found, stack, method_call);
  756.  
  757.     MethodSymbol *method_symbol = (methods_found.Length() > 0 ? methods_found[0] : (MethodSymbol *) NULL);
  758.     if (method_symbol)
  759.     {
  760.         for (int i = 1; i < methods_found.Length(); i++)
  761.         {
  762.             ReportSemError(SemanticError::AMBIGUOUS_METHOD_INVOCATION,
  763.                            method_call -> LeftToken(),
  764.                            method_call -> RightToken(),
  765.                            method_symbol -> Name(),
  766.                            methods_found[0] -> Header(),
  767.                            method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  768.                            method_symbol -> containing_type -> ExternalName(),
  769.                            methods_found[i] -> Header(),
  770.                            methods_found[i] -> containing_type -> ContainingPackage() -> PackageName(),
  771.                            methods_found[i] -> containing_type -> ExternalName());
  772.         }
  773.  
  774.         if (method_symbol -> containing_type != where_found -> Type())  // is symbol an inherited field?
  775.         {
  776.             if (method_symbol -> IsSynthetic())
  777.             {
  778.                 ReportSemError(SemanticError::SYNTHETIC_METHOD_INVOCATION,
  779.                                method_call -> LeftToken(),
  780.                                method_call -> RightToken(),
  781.                                method_symbol -> Header(),
  782.                                method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  783.                                method_symbol -> containing_type -> ExternalName());
  784.             }
  785.             else
  786.             {
  787.                 for (SemanticEnvironment *env = where_found -> previous; env; env = env -> previous)
  788.                 {
  789.                     Tuple<MethodSymbol *> others(2);
  790.                     SemanticEnvironment *found_other;
  791.                     SearchForMethodInEnvironment(others, found_other, env, method_call);
  792.  
  793.                     int i;
  794.                     for (i = 0; i < others.Length();  i++)
  795.                     {
  796.                         if (others[i] != method_symbol)
  797.                         {
  798.                             ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
  799.                                            method_call -> LeftToken(),
  800.                                            method_call -> RightToken(),
  801.                                            method_symbol -> Name(),
  802.                                            method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  803.                                            method_symbol -> containing_type -> ExternalName(),
  804.                                            found_other -> Type() -> ContainingPackage() -> PackageName(),
  805.                                            found_other -> Type() -> ExternalName());
  806.                             break;
  807.                         }
  808.                     }
  809.  
  810.                     if (i < others.Length()) // as soon as we find an error, bail out...
  811.                         break;
  812.                 }
  813.             }
  814.         }
  815.     }
  816.     else
  817.     {
  818.         AstSimpleName *simple_name = method_call -> method -> SimpleNameCast();
  819.         NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  820.         bool symbol_found = false;
  821.  
  822.         //
  823.         // First, search for a perfect visible method match in an enclosing class.
  824.         //
  825.         assert(stack);
  826.  
  827.         for (SemanticEnvironment *env = stack -> previous; env; env = env -> previous)
  828.         {
  829.             Tuple<MethodSymbol *> others(2);
  830.             SemanticEnvironment *found_other;
  831.             SearchForMethodInEnvironment(others, found_other, env, method_call);
  832.  
  833.             if (others.Length() > 0)
  834.             {
  835.                 ReportSemError(SemanticError::HIDDEN_METHOD_IN_ENCLOSING_CLASS,
  836.                                method_call -> LeftToken(),
  837.                                method_call -> RightToken(),
  838.                                others[0] -> Header(),
  839.                                others[0] -> containing_type -> ContainingPackage() -> PackageName(),
  840.                                others[0] -> containing_type -> ExternalName());
  841.  
  842.                 symbol_found = true;
  843.                 break;
  844.             }
  845.         }
  846.  
  847.         //
  848.         // If a method in an enclosing class was not found. Search for a similar visible field
  849.         // or a private method with the name.
  850.         //
  851.         for (SemanticEnvironment *env2 = stack; env2 && (! symbol_found); env2 = env2 -> previous)
  852.         {
  853.             TypeSymbol *type = env2 -> Type();
  854.  
  855.             if (! type -> expanded_field_table)
  856.                 ComputeFieldsClosure(type, simple_name -> identifier_token);
  857.  
  858.             VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  859.             if (variable_shadow_symbol)
  860.             {
  861.                 VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  862.                 TypeSymbol *enclosing_type = variable_symbol -> owner -> TypeCast();
  863.  
  864.                 assert(enclosing_type);
  865.  
  866.                 ReportSemError(SemanticError::FIELD_NOT_METHOD,
  867.                                method_call -> LeftToken(),
  868.                                method_call -> RightToken(),
  869.                                variable_symbol -> Name(),
  870.                                enclosing_type -> ContainingPackage() -> PackageName(),
  871.                                enclosing_type -> ExternalName());
  872.                 symbol_found = true;
  873.                 break;
  874.             }
  875.             else
  876.             {
  877.                 TypeSymbol *super_type;
  878.                 MethodShadowSymbol *method_shadow;
  879.  
  880.                 for (super_type = type -> super; super_type; super_type = super_type -> super)
  881.                 {
  882.                     for (method_shadow = super_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  883.                          method_shadow; method_shadow = method_shadow -> next_method)
  884.                     {
  885.                         MethodSymbol *method = method_shadow -> method_symbol;
  886.                         if (! method -> IsTyped())
  887.                             method -> ProcessMethodSignature((Semantic *) this, simple_name -> identifier_token);
  888.  
  889.                         if (method_call -> NumArguments() == method -> NumFormalParameters())
  890.                         {
  891.                             int i;
  892.                             for (i = 0; i < method_call -> NumArguments(); i++)
  893.                             {
  894.                                 AstExpression *expr = method_call -> Argument(i);
  895.                                 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  896.                                     break;
  897.                             }
  898.                             if (i == method_call -> NumArguments()) // found a match ?
  899.                                 break;
  900.                         }
  901.                     }
  902.  
  903.                     if (method_shadow) // found a match ?
  904.                         break;
  905.                 }
  906.  
  907.                 if (super_type)
  908.                 {
  909.                     ReportSemError((method_shadow -> method_symbol -> ACC_PRIVATE()
  910.                                                    ? SemanticError::METHOD_WITH_PRIVATE_ACCESS_NOT_ACCESSIBLE
  911.                                                    : SemanticError::METHOD_WITH_DEFAULT_ACCESS_NOT_ACCESSIBLE),
  912.                                    method_call -> LeftToken(),
  913.                                    method_call -> RightToken(),
  914.                                    name_symbol -> Name(),
  915.                                    super_type -> ContainingPackage() -> PackageName(),
  916.                                    super_type -> ExternalName(),
  917.                                    type -> ContainingPackage() -> PackageName(),
  918.                                    type -> ExternalName());
  919.                     symbol_found = true;
  920.                     break;
  921.                 }
  922.             }
  923.         }
  924.  
  925.         //
  926.         // Finally, if we did not find a method or field name that matches, look for a type
  927.         // with that name.
  928.         //
  929.         if (! symbol_found)
  930.         {
  931.             TypeSymbol *this_type = ThisType();
  932.  
  933.             if (FindType(simple_name -> identifier_token))
  934.             {
  935.                 ReportSemError(SemanticError::TYPE_NOT_METHOD,
  936.                                simple_name -> identifier_token,
  937.                                simple_name -> identifier_token,
  938.                                name_symbol -> Name());
  939.             }
  940.             else if (this_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol))
  941.                 ReportMethodNotFound(method_call, name_symbol -> Name());
  942.             else
  943.             {
  944.                 MethodSymbol *method = FindMisspelledMethodName(this_type, method_call, name_symbol);
  945.                 if (method)
  946.                      ReportSemError(SemanticError::METHOD_NAME_MISSPELLED,
  947.                                     method_call -> LeftToken(),
  948.                                     method_call -> RightToken(),
  949.                                     name_symbol -> Name(),
  950.                                     this_type -> ContainingPackage() -> PackageName(),
  951.                                     this_type -> ExternalName(),
  952.                                     method -> Name());
  953.                 else ReportSemError(SemanticError::METHOD_NAME_NOT_FOUND_IN_TYPE,
  954.                                     method_call -> LeftToken(),
  955.                                     method_call -> RightToken(),
  956.                                     name_symbol -> Name(),
  957.                                     this_type -> ContainingPackage() -> PackageName(),
  958.                                     this_type -> ExternalName());
  959.             }
  960.         }
  961.     }
  962.  
  963.     //
  964.     // If this method came from a class file, make sure that its throws clause has been processed.
  965.     //
  966.     if (method_symbol)
  967.     {
  968.         method_symbol -> ProcessMethodThrows((Semantic *) this, method_call -> method -> RightToken());
  969.  
  970.         if (control.option.deprecation &&
  971.             method_symbol -> IsDeprecated() && method_symbol -> containing_type -> outermost_type != ThisType() -> outermost_type)
  972.         {
  973.             ReportSemError(SemanticError::DEPRECATED_METHOD,
  974.                            method_call -> LeftToken(),
  975.                            method_call -> RightToken(),
  976.                            method_symbol -> Header(),
  977.                            method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  978.                            method_symbol -> containing_type -> ExternalName());
  979.         }
  980.     }
  981.  
  982.     return method_symbol;
  983. }
  984.  
  985.  
  986.  
  987. //
  988. // Search the type in question for a variable. Note that name_symbol is an optional argument.
  989. // If it was not passed to this function then its default value is NULL (see semantic.h) and
  990. // we assume that the name to search for is the last identifier specified in the field_access.
  991. //
  992. inline VariableSymbol *Semantic::FindVariableInType(TypeSymbol *type, AstFieldAccess *field_access, NameSymbol *name_symbol)
  993. {
  994.     if (! name_symbol)
  995.         name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  996.  
  997.     if (! type -> expanded_field_table)
  998.         ComputeFieldsClosure(type, field_access -> identifier_token);
  999.  
  1000. //
  1001. // TODO: Confirm that this is no longer the case as of javac 1.2
  1002. //
  1003. /*
  1004.     //
  1005.     // First look for the variable in the "type". If it is not found, look for
  1006.     // it in the superclasses in the proper order. It is possible that the
  1007.     // field in question is a private field that is contained in the body
  1008.     // of the type that we are currently processing (this_type()), in which case
  1009.     // it is accessible even though it is not directly inherited by "type".
  1010.     //
  1011.     VariableShadowSymbol *variable_shadow_symbol;
  1012.     for (variable_shadow_symbol = NULL; (! variable_shadow_symbol) && type; type = type -> super)
  1013.         variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1014. */
  1015.  
  1016.     VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1017.  
  1018.     //
  1019.     // Recall that even an inaccessible member x of a super class (or interface) S,
  1020.     // in addition to not been inherited by a subclass, hides all other occurrences of x that may
  1021.     // appear in a super class (or super interface) of S (see 8.3).
  1022.     //
  1023.     if (variable_shadow_symbol)
  1024.     {
  1025.         VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  1026.  
  1027.         for (int i = 0; i < variable_shadow_symbol -> NumConflicts(); i++)
  1028.         {
  1029.             ReportSemError(SemanticError::AMBIGUOUS_FIELD,
  1030.                            field_access -> LeftToken(),
  1031.                            field_access -> RightToken(),
  1032.                            name_symbol -> Name(),
  1033.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1034.                            variable_symbol -> owner -> TypeCast() -> ExternalName(),
  1035.                            variable_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1036.                            variable_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ExternalName());
  1037.         }
  1038.  
  1039.         if (variable_symbol -> IsSynthetic())
  1040.         {
  1041.             ReportSemError(SemanticError::SYNTHETIC_VARIABLE_ACCESS,
  1042.                            field_access -> LeftToken(),
  1043.                            field_access -> RightToken(),
  1044.                            variable_symbol -> Name(),
  1045.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1046.                            variable_symbol -> owner -> TypeCast() -> ExternalName());
  1047.         }
  1048.  
  1049.         if (control.option.deprecation &&
  1050.             variable_symbol -> IsDeprecated() &&
  1051.             variable_symbol -> owner -> TypeCast() -> outermost_type != ThisType() -> outermost_type)
  1052.         {
  1053.             ReportSemError(SemanticError::DEPRECATED_FIELD,
  1054.                            field_access -> LeftToken(),
  1055.                            field_access -> RightToken(),
  1056.                            variable_symbol -> Name(),
  1057.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1058.                            variable_symbol -> owner -> TypeCast() -> ExternalName());
  1059.         }
  1060.  
  1061.         if (! variable_symbol -> IsTyped())
  1062.             variable_symbol -> ProcessVariableSignature((Semantic *) this, field_access -> identifier_token);
  1063.  
  1064.         return variable_symbol;
  1065.     }
  1066.  
  1067.     return (VariableSymbol *) NULL;
  1068. }
  1069.  
  1070.  
  1071. void Semantic::ReportAccessedFieldNotFound(AstFieldAccess *field_access, TypeSymbol *type)
  1072. {
  1073.     NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  1074.     VariableShadowSymbol *variable_shadow_symbol;
  1075.  
  1076.     if (! type -> expanded_field_table)
  1077.         ComputeFieldsClosure(type, field_access -> base -> LeftToken());
  1078.     TypeSymbol *super_type;
  1079.     for (super_type = type -> super; super_type; super_type = super_type -> super)
  1080.     {
  1081.         variable_shadow_symbol = super_type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1082.         if (variable_shadow_symbol)
  1083.             break;
  1084.     }
  1085.  
  1086.     if (super_type)
  1087.     {
  1088.         VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  1089.         ReportSemError((variable_symbol -> ACC_PRIVATE()
  1090.                                          ? SemanticError::FIELD_WITH_PRIVATE_ACCESS_NOT_ACCESSIBLE
  1091.                                          : SemanticError::FIELD_WITH_DEFAULT_ACCESS_NOT_ACCESSIBLE),
  1092.                        field_access -> LeftToken(),
  1093.                        field_access -> RightToken(),
  1094.                        variable_symbol -> Name(),
  1095.                        super_type -> ContainingPackage() -> PackageName(),
  1096.                        super_type -> ExternalName(),
  1097.                        type -> ContainingPackage() -> PackageName(),
  1098.                        type -> ExternalName());
  1099.     }
  1100.     else
  1101.     {
  1102.         VariableSymbol *variable = FindMisspelledVariableName(type, field_access -> identifier_token);
  1103.         if (variable)
  1104.              ReportSemError(SemanticError::FIELD_NAME_MISSPELLED,
  1105.                             field_access -> LeftToken(),
  1106.                             field_access -> RightToken(),
  1107.                             name_symbol -> Name(),
  1108.                             type -> ContainingPackage() -> PackageName(),
  1109.                             type -> ExternalName(),
  1110.                             variable -> Name());
  1111.         else ReportSemError(SemanticError::FIELD_NOT_FOUND,
  1112.                             field_access -> LeftToken(),
  1113.                             field_access -> RightToken(),
  1114.                             lex_stream -> NameString(field_access -> identifier_token),
  1115.                             type -> ContainingPackage() -> PackageName(),
  1116.                             type -> ExternalName());
  1117.     }
  1118.  
  1119.     return;
  1120. }
  1121.  
  1122.  
  1123. void Semantic::SearchForVariableInEnvironment(Tuple<VariableSymbol *> &variables_found,
  1124.                                               SemanticEnvironment *&where_found,
  1125.                                               SemanticEnvironment *stack,
  1126.                                               NameSymbol *name_symbol,
  1127.                                               LexStream::TokenIndex identifier_token)
  1128. {
  1129.     variables_found.Reset();
  1130.     where_found = (SemanticEnvironment *) NULL;
  1131.  
  1132.     for (SemanticEnvironment *env = stack; env; env = env -> previous)
  1133.     {
  1134.         VariableSymbol *variable_symbol = env -> symbol_table.FindVariableSymbol(name_symbol);
  1135.         if (variable_symbol) // a local variable
  1136.         {
  1137.             variables_found.Next() = variable_symbol;
  1138.             where_found = env;
  1139.             break;
  1140.         }
  1141.  
  1142.         TypeSymbol *type = env -> Type();
  1143.         if (! type -> expanded_field_table)
  1144.             ComputeFieldsClosure(type, identifier_token);
  1145.         VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1146.         if (variable_shadow_symbol)
  1147.         {
  1148.             //
  1149.             // Since type -> IsOwner(this_type()), i.e., type encloses this_type(),
  1150.             // variable_symbol is accessible, even if it is private.
  1151.             //
  1152.             variables_found.Next() = variable_shadow_symbol -> variable_symbol;
  1153.  
  1154.             //
  1155.             // Recall that even an inaccessible member x of a super class (or interface) S,
  1156.             // in addition to not been inherited by a subclass, hides all other occurrences of x that may
  1157.             // appear in a super class (or super interface) of S (see 8.3).
  1158.             //
  1159.             for (int i = 0; i < variable_shadow_symbol -> NumConflicts(); i++)
  1160.                 variables_found.Next() = variable_shadow_symbol -> Conflict(i);
  1161.             where_found = env;
  1162.             break;
  1163.         }
  1164.     }
  1165.  
  1166.     return;
  1167. }
  1168.  
  1169.  
  1170. VariableSymbol *Semantic::FindVariableInEnvironment(SemanticEnvironment *&where_found,
  1171.                                                     SemanticEnvironment *stack, LexStream::TokenIndex identifier_token)
  1172. {
  1173.     Tuple<VariableSymbol *> variables_found(2);
  1174.     NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  1175.     SearchForVariableInEnvironment(variables_found, where_found, stack, name_symbol, identifier_token);
  1176.  
  1177.     VariableSymbol *variable_symbol = (VariableSymbol *) (variables_found.Length() > 0 ? variables_found[0] : NULL);
  1178.  
  1179.     if (variable_symbol)
  1180.     {
  1181.         if (variable_symbol -> IsLocal()) // a local variable
  1182.         {
  1183.             if (where_found != stack)
  1184.             {
  1185.                 TypeSymbol *type = stack -> Type();
  1186.  
  1187.                 if (! variable_symbol -> ACC_FINAL())
  1188.                 {
  1189.                     MethodSymbol *method = variable_symbol -> owner -> MethodCast();
  1190.  
  1191.                     ReportSemError(SemanticError::INNER_CLASS_REFERENCE_TO_NON_FINAL_LOCAL_VARIABLE,
  1192.                                    identifier_token,
  1193.                                    identifier_token,
  1194.                                    type -> ContainingPackage() -> PackageName(),
  1195.                                    type -> ExternalName(),
  1196.                                    lex_stream -> NameString(identifier_token),
  1197.                                    //
  1198.                                    // TODO: What if the method is a constructor ?
  1199.                                    //        if (method -> Identity() != control.init_symbol &&
  1200.                                    //            method -> Identity() != control.block_init_symbol &&
  1201.                                    //            method -> Identity() != control.clinit_symbol)
  1202.                                    //
  1203.                                    //
  1204.                                    method -> Name());
  1205.                 }
  1206.  
  1207.                 //
  1208.                 // Insert a local shadow in the type. If we are currently processing a
  1209.                 // constructor, the local shadow would have been passed to it as an argument.
  1210.                 // If so, use the local argument; otherwise, use the local shadow.
  1211.                 //
  1212.                 VariableSymbol *local_shadow = type -> FindOrInsertLocalShadow(variable_symbol),
  1213.                                *local_symbol = stack -> symbol_table.FindVariableSymbol(local_shadow -> Identity());
  1214.                 variable_symbol = (local_symbol ? local_symbol : local_shadow);
  1215.  
  1216.                 assert(variable_symbol);
  1217.  
  1218.                 where_found = stack;
  1219.             }
  1220.         }
  1221.         else if (variable_symbol -> owner != where_found -> Type())  // is symbol an inherited field?
  1222.         {
  1223.             TypeSymbol *type = (TypeSymbol *) variable_symbol -> owner;
  1224.  
  1225.             if (variable_symbol -> IsSynthetic())
  1226.             {
  1227.                 ReportSemError(SemanticError::SYNTHETIC_VARIABLE_ACCESS,
  1228.                                identifier_token,
  1229.                                identifier_token,
  1230.                                variable_symbol -> Name(),
  1231.                                type -> ContainingPackage() -> PackageName(),
  1232.                                type -> ExternalName());
  1233.             }
  1234.             else
  1235.             {
  1236.                 for (SemanticEnvironment *env = where_found -> previous; env; env = env -> previous)
  1237.                 {
  1238.                     Tuple<VariableSymbol *> others(2);
  1239.                     SemanticEnvironment *found_other;
  1240.                     SearchForVariableInEnvironment(others, found_other, env, name_symbol, identifier_token);
  1241.  
  1242.                     int i;
  1243.                     for (i = 0; i < others.Length();  i++)
  1244.                     {
  1245.                         if (others[i] != variable_symbol)
  1246.                         {
  1247.                             MethodSymbol *method = others[i] -> owner -> MethodCast();
  1248.  
  1249.                             if (method)
  1250.                             {
  1251.                                 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_LOCAL,
  1252.                                                identifier_token,
  1253.                                                identifier_token,
  1254.                                                lex_stream -> NameString(identifier_token),
  1255.                                                type -> ContainingPackage() -> PackageName(),
  1256.                                                type -> ExternalName(),
  1257.                                                method -> Name());
  1258.                                 break;
  1259.                             }
  1260.                             else
  1261.                             {
  1262.                                 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
  1263.                                                identifier_token,
  1264.                                                identifier_token,
  1265.                                                lex_stream -> NameString(identifier_token),
  1266.                                                type -> ContainingPackage() -> PackageName(),
  1267.                                                type -> ExternalName(),
  1268.                                                found_other -> Type() -> ContainingPackage() -> PackageName(),
  1269.                                                found_other -> Type() -> ExternalName());
  1270.                                 break;
  1271.                             }
  1272.                         }
  1273.                     }
  1274.  
  1275.                     if (i < others.Length()) // as soon as we find an error, bail out...
  1276.                         break;
  1277.                 }
  1278.             }
  1279.         }
  1280.     }
  1281.  
  1282.     for (int i = 1; i < variables_found.Length(); i++)
  1283.     {
  1284.         ReportSemError(SemanticError::AMBIGUOUS_FIELD,
  1285.                        identifier_token,
  1286.                        identifier_token,
  1287.                        variable_symbol -> Name(),
  1288.                        variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1289.                        variable_symbol -> owner -> TypeCast() -> ExternalName(),
  1290.                        variables_found[i] -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1291.                        variables_found[i] -> owner -> TypeCast() -> ExternalName());
  1292.     }
  1293.  
  1294.     if (variable_symbol)
  1295.     {
  1296.         if (control.option.deprecation &&
  1297.             variable_symbol -> IsDeprecated() &&
  1298.             variable_symbol -> owner -> TypeCast() -> outermost_type != ThisType() -> outermost_type)
  1299.         {
  1300.             ReportSemError(SemanticError::DEPRECATED_FIELD,
  1301.                            identifier_token,
  1302.                            identifier_token,
  1303.                            variable_symbol -> Name(),
  1304.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1305.                            variable_symbol -> owner -> TypeCast() -> ExternalName());
  1306.         }
  1307.  
  1308.         if (! variable_symbol -> IsTyped())
  1309.             variable_symbol -> ProcessVariableSignature((Semantic *) this, identifier_token);
  1310.     }
  1311.  
  1312.     return variable_symbol;
  1313. }
  1314.  
  1315.  
  1316. VariableSymbol *Semantic::FindInstance(TypeSymbol *base_type, TypeSymbol *environment_type)
  1317. {
  1318.     for (int i = 0; i < base_type -> NumEnclosingInstances(); i++)
  1319.     {
  1320.         VariableSymbol *variable = base_type -> EnclosingInstance(i);
  1321.         if (variable -> Type() -> IsSubclass(environment_type))
  1322.             return variable;
  1323.     }
  1324.  
  1325.     AstClassDeclaration *class_declaration = base_type -> declaration -> ClassDeclarationCast();
  1326.     AstClassInstanceCreationExpression *class_creation = base_type -> declaration -> ClassInstanceCreationExpressionCast();
  1327.  
  1328.     assert(class_declaration || class_creation);
  1329.  
  1330.     AstClassBody *class_body = (class_declaration ? class_declaration -> class_body : class_creation -> class_body_opt);
  1331.  
  1332.     LexStream::TokenIndex loc = class_body -> left_brace_token;
  1333.     AstBlock *this_block = class_body -> this_block;
  1334.     if (! this_block)
  1335.     {
  1336.         this_block = compilation_unit -> ast_pool -> GenBlock();
  1337.         this_block -> left_brace_token  = loc;
  1338.         this_block -> right_brace_token = loc;
  1339.  
  1340.         this_block -> is_reachable = true;
  1341.         this_block -> can_complete_normally = true;
  1342.  
  1343.         class_body -> this_block = this_block;
  1344.     }
  1345.  
  1346.     int k = base_type -> NumEnclosingInstances();
  1347.     for (TypeSymbol *type = base_type -> EnclosingInstance(k - 1) -> Type(); type; type = type -> ContainingType(), k++)
  1348.     {
  1349.         AstSimpleName *parm = compilation_unit -> ast_pool -> GenSimpleName(loc);
  1350.         parm -> symbol = base_type -> EnclosingInstance(k - 1);
  1351.  
  1352.         VariableSymbol *variable_symbol = (VariableSymbol *) type -> FindThis(0);
  1353.  
  1354.         AstFieldAccess *method_name = compilation_unit -> ast_pool -> GenFieldAccess();
  1355.         method_name -> base = parm;
  1356.         method_name -> dot_token = loc;
  1357.         method_name -> identifier_token = loc;
  1358.         method_name -> symbol = variable_symbol; // the variable in question
  1359.  
  1360.         AstMethodInvocation *rhs       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1361.         rhs -> method                  = method_name;
  1362.         rhs -> left_parenthesis_token  = loc;
  1363.         rhs -> right_parenthesis_token = loc;
  1364.         rhs -> symbol                  = TypeSymbol::GetReadAccessMethod(variable_symbol);
  1365.         rhs -> AddArgument(parm); // TODO: WARNING: sharing of Ast subtree !!!
  1366.  
  1367.  
  1368.         AstSimpleName *lhs = compilation_unit -> ast_pool -> GenSimpleName(loc);
  1369.         VariableSymbol *variable = base_type -> FindThis(k);
  1370.         lhs -> symbol = (variable ? variable : base_type -> InsertThis(k));
  1371.  
  1372.         AstAssignmentExpression *assign = compilation_unit -> ast_pool
  1373.                                                            -> GenAssignmentExpression(AstAssignmentExpression::EQUAL, loc);
  1374.         assign -> left_hand_side = lhs;
  1375.         assign -> expression     = rhs;
  1376.         assign -> symbol         = lhs -> Type();
  1377.  
  1378.         AstExpressionStatement *stmt  = compilation_unit -> ast_pool -> GenExpressionStatement();
  1379.         stmt -> expression            = assign;
  1380.         stmt -> semicolon_token_opt   = loc;
  1381.         stmt -> is_reachable          = true;
  1382.         stmt -> can_complete_normally = true;
  1383.  
  1384.         this_block -> AddStatement(stmt);
  1385.  
  1386.         if (lhs -> Type() -> IsSubclass(environment_type))
  1387.             break;
  1388.     }
  1389.  
  1390.     return base_type -> EnclosingInstance(k);
  1391. }
  1392.  
  1393.  
  1394. AstExpression *Semantic::CreateAccessToType(Ast *source, TypeSymbol *environment_type)
  1395. {
  1396.     TypeSymbol *this_type = ThisType();
  1397.  
  1398.     LexStream::TokenIndex tok;
  1399.  
  1400.     AstSimpleName *simple_name = source -> SimpleNameCast();
  1401.     AstFieldAccess *field_access = source -> FieldAccessCast();
  1402.     AstSuperCall *super_call = source -> SuperCallCast();
  1403.     AstThisCall *this_call = source -> ThisCallCast();
  1404.     AstClassInstanceCreationExpression *class_creation = source -> ClassInstanceCreationExpressionCast();
  1405.  
  1406.     if (simple_name)
  1407.          tok = simple_name -> identifier_token;
  1408.     else if (class_creation)
  1409.          tok = class_creation -> new_token;
  1410.     else if (super_call)
  1411.          tok = super_call -> super_token;
  1412.     else if (this_call)
  1413.          tok = this_call -> this_token;
  1414.     else if (field_access)
  1415.          tok = field_access -> dot_token;
  1416.     else assert(false);
  1417.  
  1418.     AstExpression *resolution;
  1419.  
  1420.     if (! this_type -> CanAccess(environment_type))
  1421.     {
  1422.         ReportSemError(SemanticError::ENCLOSING_INSTANCE_NOT_ACCESSIBLE,
  1423.                        (field_access ? field_access -> base -> LeftToken() : tok),
  1424.                        (field_access ? field_access -> base -> RightToken() : tok),
  1425.                        environment_type -> ContainingPackage() -> PackageName(),
  1426.                        environment_type -> ExternalName());
  1427.  
  1428.         resolution = compilation_unit -> ast_pool -> GenSimpleName(tok);
  1429.         resolution -> symbol = control.no_type;
  1430.     }
  1431.     else if (ExplicitConstructorInvocation())
  1432.     {
  1433.         VariableSymbol *variable = LocalSymbolTable().FindVariableSymbol(control.this0_name_symbol);
  1434.  
  1435.         assert(variable);
  1436.  
  1437.         resolution = compilation_unit -> ast_pool -> GenSimpleName(tok);
  1438.         resolution -> symbol = variable;
  1439.  
  1440.         //
  1441.         // TODO: Document this !!!
  1442.         //
  1443.         if (ExplicitConstructorInvocation() -> SuperCallCast())
  1444.         {
  1445.             TypeSymbol *containing_type = this_type -> ContainingType();
  1446.             if (! containing_type -> IsSubclass(environment_type))
  1447.             {
  1448.                 variable = FindInstance(containing_type, environment_type);
  1449.  
  1450.                 AstFieldAccess *method_name = compilation_unit -> ast_pool -> GenFieldAccess();
  1451.                 method_name -> base = resolution; // TODO: WARNING: sharing of Ast subtree !!!
  1452.                 method_name -> dot_token = tok;
  1453.                 method_name -> identifier_token = tok;
  1454.                 method_name -> symbol = variable;
  1455.  
  1456.                 AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1457.                 method_call -> method                  = method_name;
  1458.                 method_call -> left_parenthesis_token  = tok;
  1459.                 method_call -> right_parenthesis_token = tok;
  1460.  
  1461.                 assert(containing_type == variable -> owner -> TypeCast());
  1462.  
  1463.                 method_call -> symbol = TypeSymbol::GetReadAccessMethod(variable);
  1464.                 method_call -> AddArgument(resolution);
  1465.  
  1466.                 resolution = method_call;
  1467.             }
  1468.         }
  1469.     }
  1470.     else if (this_type -> IsSubclass(environment_type))
  1471.     {
  1472.         resolution = compilation_unit -> ast_pool -> GenThisExpression(tok);
  1473.         resolution -> symbol = this_type;
  1474.     }
  1475.     else
  1476.     {
  1477.         resolution = compilation_unit -> ast_pool -> GenSimpleName(tok);
  1478.         resolution -> symbol = FindInstance(this_type, environment_type);
  1479.     }
  1480.  
  1481.     return ((resolution -> symbol == control.no_type) || (resolution -> Type() == environment_type)
  1482.                                                        ? resolution
  1483.                                                        : ConvertToType(resolution, environment_type));
  1484. }
  1485.  
  1486.  
  1487. void Semantic::CreateAccessToScopedVariable(AstSimpleName *simple_name, TypeSymbol *environment_type)
  1488. {
  1489.     assert(environment_type -> IsOwner(ThisType()));
  1490.  
  1491.     VariableSymbol *variable_symbol = (VariableSymbol *) simple_name -> symbol;
  1492.  
  1493.     AstExpression *access_expression;
  1494.     if (variable_symbol -> ACC_STATIC())
  1495.     {
  1496.         access_expression = compilation_unit -> ast_pool -> GenSimpleName(simple_name -> identifier_token);
  1497.         access_expression -> symbol = environment_type;
  1498.     }
  1499.     else access_expression = CreateAccessToType(simple_name, environment_type);
  1500.  
  1501.     if (access_expression -> symbol != control.no_type)
  1502.     {
  1503.         //
  1504.         // TODO: we have filed a query to Sun regarding the necessity of this check!
  1505.         //
  1506.         // SimpleNameAccessCheck(simple_name, variable_symbol -> owner -> TypeCast(), variable_symbol);
  1507.         //
  1508.         if (variable_symbol -> ACC_PRIVATE())
  1509.         {
  1510.             AstFieldAccess *method_name     = compilation_unit -> ast_pool -> GenFieldAccess();
  1511.             method_name -> base             = access_expression;
  1512.             method_name -> dot_token        = simple_name -> identifier_token;
  1513.             method_name -> identifier_token = simple_name -> identifier_token;
  1514.             method_name -> symbol           = variable_symbol;
  1515.  
  1516.             AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1517.             method_call -> method                  = method_name;
  1518.             method_call -> left_parenthesis_token  = simple_name -> identifier_token;
  1519.             method_call -> right_parenthesis_token = simple_name -> identifier_token;
  1520.             method_call -> symbol                  = TypeSymbol::GetReadAccessMethod(variable_symbol);
  1521.  
  1522.             if (! variable_symbol -> ACC_STATIC())
  1523.                 method_call -> AddArgument(access_expression); // TODO: WARNING: sharing of Ast subtree !!!
  1524.  
  1525.             simple_name -> resolution_opt = method_call;
  1526.         }
  1527.         else
  1528.         {
  1529.             AstFieldAccess *field_access     = compilation_unit -> ast_pool -> GenFieldAccess();
  1530.             field_access -> base             = access_expression;
  1531.             field_access -> dot_token        = simple_name -> identifier_token;
  1532.             field_access -> identifier_token = simple_name -> identifier_token;
  1533.             field_access -> symbol           = variable_symbol;
  1534.  
  1535.             simple_name -> resolution_opt = field_access;
  1536.         }
  1537.     }
  1538.  
  1539.     return;
  1540. }
  1541.  
  1542.  
  1543. void Semantic::CreateAccessToScopedMethod(AstMethodInvocation *method_call, TypeSymbol *environment_type)
  1544. {
  1545.     assert(environment_type -> IsOwner(ThisType()));
  1546.  
  1547.     MethodSymbol *method = (MethodSymbol *) method_call -> symbol;
  1548.     AstSimpleName *simple_name = (AstSimpleName *) method_call -> method;
  1549.  
  1550.     assert(simple_name -> SimpleNameCast());
  1551.  
  1552.     AstExpression *access_expression;
  1553.     if (method -> ACC_STATIC())
  1554.     {
  1555.         access_expression = compilation_unit -> ast_pool -> GenSimpleName(simple_name -> identifier_token);
  1556.         access_expression -> symbol = environment_type;
  1557.     }
  1558.     else access_expression = CreateAccessToType(simple_name, environment_type);
  1559.  
  1560.     if (access_expression -> symbol != control.no_type)
  1561.     {
  1562.         //
  1563.         // TODO: we have filed a query to Sun regarding the necessity of this check!
  1564.         //
  1565.         // SimpleNameAccessCheck(simple_name, method -> containing_type, method);
  1566.         //
  1567.         simple_name -> resolution_opt = access_expression;
  1568.         if (method -> ACC_PRIVATE())
  1569.         {
  1570.             if (method -> ACC_STATIC())
  1571.                 method_call -> symbol = TypeSymbol::GetReadAccessMethod(method);
  1572.             else
  1573.             {
  1574.                 AstMethodInvocation *old_method_call = method_call;
  1575.  
  1576.                 //
  1577.                 // TODO: WARNING: sharing of subtrees...
  1578.                 //
  1579.                 AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1580.                 method_call -> method                  = old_method_call -> method;
  1581.                 method_call -> left_parenthesis_token  = old_method_call -> left_parenthesis_token;
  1582.                 method_call -> right_parenthesis_token = old_method_call -> right_parenthesis_token;
  1583.                 method_call -> symbol                  = TypeSymbol::GetReadAccessMethod(method);
  1584.                 method_call -> AddArgument(access_expression);
  1585.                 for (int i = 0; i < old_method_call -> NumArguments(); i++)
  1586.                     method_call -> AddArgument(old_method_call -> Argument(i));
  1587.  
  1588.                 old_method_call -> symbol = method;
  1589.                 old_method_call -> resolution_opt = method_call;
  1590.             }
  1591.         }
  1592.     }
  1593.  
  1594.     return;
  1595. }
  1596.  
  1597.  
  1598. void Semantic::CheckSimpleName(AstSimpleName *simple_name, SemanticEnvironment *where_found)
  1599. {
  1600.     VariableSymbol *variable_symbol = simple_name -> symbol -> VariableCast();
  1601.  
  1602.     assert(variable_symbol);
  1603.  
  1604.     if (StaticRegion())
  1605.     {
  1606.         if (! (variable_symbol -> IsLocal() || variable_symbol -> ACC_STATIC()))
  1607.         {
  1608.             ReportSemError(SemanticError::NAME_NOT_CLASS_VARIABLE,
  1609.                            simple_name -> identifier_token,
  1610.                            simple_name -> identifier_token,
  1611.                            lex_stream -> NameString(simple_name -> identifier_token));
  1612.         }
  1613.         else if (! variable_symbol -> IsDeclarationComplete())
  1614.         {
  1615.             ReportSemError(SemanticError::NAME_NOT_YET_AVAILABLE,
  1616.                            simple_name -> identifier_token,
  1617.                            simple_name -> identifier_token,
  1618.                            lex_stream -> NameString(simple_name -> identifier_token));
  1619.         }
  1620.     }
  1621.     else if (! variable_symbol -> ACC_STATIC()) // an instance variable?
  1622.     {
  1623.         TypeSymbol *containing_type = variable_symbol -> owner -> TypeCast(); // an instance field member ?
  1624.  
  1625.         if (containing_type) // variable must be a field
  1626.         {
  1627.             if (containing_type == ThisType() && (! variable_symbol -> IsDeclarationComplete())) // forward reference?
  1628.             {
  1629.                 ReportSemError(SemanticError::NAME_NOT_YET_AVAILABLE,
  1630.                                simple_name -> identifier_token,
  1631.                                simple_name -> identifier_token,
  1632.                                lex_stream -> NameString(simple_name -> identifier_token));
  1633.             }
  1634.             else if (ExplicitConstructorInvocation() && where_found == state_stack.Top())
  1635.             {
  1636.                 //
  1637.                 // If the variable in question is an instance variable that is
  1638.                 // declared in this_type (this_type is definitely a class) or
  1639.                 // one of its super classes, then we have an error:
  1640.                 //
  1641.                 ReportSemError(SemanticError::INSTANCE_VARIABLE_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  1642.                                simple_name -> identifier_token,
  1643.                                simple_name -> identifier_token,
  1644.                                lex_stream -> NameString(simple_name -> identifier_token),
  1645.                                containing_type -> Name());
  1646.             }
  1647.         }
  1648.     }
  1649.  
  1650.     return;
  1651. }
  1652.  
  1653.  
  1654. void Semantic::ProcessSimpleName(Ast *expr)
  1655. {
  1656.     TypeSymbol *this_type = ThisType();
  1657.  
  1658.     AstSimpleName *simple_name = (AstSimpleName *) expr;
  1659.     SemanticEnvironment *where_found;
  1660.     VariableSymbol *variable_symbol = FindVariableInEnvironment(where_found, state_stack.Top(), simple_name -> identifier_token);
  1661.     if (variable_symbol)
  1662.     {
  1663.         simple_name -> symbol = variable_symbol;
  1664.  
  1665.         assert(variable_symbol -> IsTyped());
  1666.  
  1667.         //
  1668.         // A variable_symbol FINAL must have an initial value.
  1669.         //
  1670.         if (variable_symbol -> ACC_FINAL())
  1671.         {
  1672.             if (variable_symbol -> IsDeclarationComplete())
  1673.                 simple_name -> value = variable_symbol -> initial_value;
  1674.             else if (variable_symbol -> declarator)
  1675.             {
  1676.                 AstVariableDeclarator *declarator = variable_symbol -> declarator -> VariableDeclaratorCast();
  1677.                 //
  1678.                 // If the variable declarator in question exists and its computation is not
  1679.                 // pending (to avoid looping) and it has a simple expression initializer.
  1680.                 //
  1681.                 if (declarator &&
  1682.                     (! declarator -> pending) &&
  1683.                     declarator -> variable_initializer_opt &&
  1684.                     (! declarator -> variable_initializer_opt -> ArrayInitializerCast()))
  1685.                 {
  1686.                     TypeSymbol *type = (TypeSymbol *) variable_symbol -> owner;
  1687.                     Semantic *sem = type -> semantic_environment -> sem;
  1688.                     simple_name -> value = sem -> ComputeFinalValue(declarator);
  1689.                 }
  1690.             }
  1691.         }
  1692.  
  1693.         CheckSimpleName(simple_name, where_found);
  1694.  
  1695.         //
  1696.         // If the variable belongs to an outer type, add the proper
  1697.         // pointer dereferences (and method access in the case of a
  1698.         // private variable) necessary to get to it.
  1699.         //
  1700.         if (where_found != state_stack.Top())
  1701.             CreateAccessToScopedVariable(simple_name, where_found -> Type());
  1702.     }
  1703.     else
  1704.     {
  1705.         //
  1706.         // We make a little effort to issue a better error message if we can identify
  1707.         // the name in question as the name of a method in the local type.
  1708.         //
  1709.         NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  1710.  
  1711.         MethodShadowSymbol *method_shadow;
  1712.         MethodSymbol *method;
  1713.  
  1714.         for (method_shadow = this_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  1715.              method_shadow; method_shadow = method_shadow -> next_method)
  1716.         {
  1717.             method = method_shadow -> method_symbol;
  1718.             TypeSymbol *containing_type = method -> containing_type;
  1719.  
  1720.             //
  1721.             // Make sure that method has been fully prepared
  1722.             //
  1723.             if (! method -> IsTyped())
  1724.                 method -> ProcessMethodSignature((Semantic *) this, simple_name -> identifier_token);
  1725.  
  1726.             if (method -> NumFormalParameters() == 0)
  1727.                 break;
  1728.         }
  1729.  
  1730.         if (method_shadow)
  1731.         {
  1732.              ReportSemError(SemanticError::METHOD_NOT_FIELD,
  1733.                             simple_name -> identifier_token,
  1734.                             simple_name -> identifier_token,
  1735.                             lex_stream -> NameString(simple_name -> identifier_token),
  1736.                             method -> containing_type -> ContainingPackage() -> PackageName(),
  1737.                             method -> containing_type -> ExternalName());
  1738.         }
  1739.         else if (FindType(simple_name -> identifier_token))
  1740.         {
  1741.              ReportSemError(SemanticError::TYPE_NOT_FIELD,
  1742.                             simple_name -> identifier_token,
  1743.                             simple_name -> identifier_token,
  1744.                             lex_stream -> NameString(simple_name -> identifier_token));
  1745.         }
  1746.         else
  1747.         {
  1748.             VariableSymbol *variable = FindMisspelledVariableName(this_type, simple_name -> identifier_token);
  1749.             if (variable)
  1750.                  ReportSemError(SemanticError::FIELD_NAME_MISSPELLED,
  1751.                                 simple_name -> identifier_token,
  1752.                                 simple_name -> identifier_token,
  1753.                                 name_symbol -> Name(),
  1754.                                 this_type -> ContainingPackage() -> PackageName(),
  1755.                                 this_type -> ExternalName(),
  1756.                                 variable -> Name());
  1757.             else ReportSemError(SemanticError::NAME_NOT_FOUND,
  1758.                                 simple_name -> identifier_token,
  1759.                                 simple_name -> identifier_token,
  1760.                                 lex_stream -> NameString(simple_name -> identifier_token));
  1761.         }
  1762.         simple_name -> symbol = control.no_type;
  1763.     }
  1764.  
  1765.     return;
  1766. }
  1767.  
  1768.  
  1769. void Semantic::TypeAccessCheck(Ast *ast, TypeSymbol *type)
  1770. {
  1771.     //
  1772.     // Unless we are processing the body of a type do not do type checking.
  1773.     // (This method may be invoked, for example, when FindFirstType invokes ProcessPackageOrType)
  1774.     //
  1775.     if (state_stack.Size() > 0)
  1776.     {
  1777.         TypeSymbol *this_type = ThisType();
  1778.  
  1779.         //
  1780.         // Type checking is necessary only for two types that are not enclosed within
  1781.         // the same outermost type. Note that if we are trying to access an inner type
  1782.         // T1.T2...Tn from this type, TypeAccessCheck is expected to be invoked first
  1783.         // for T1, then T1.T2, ... and finally for T1.T2...Tn, in turn. When invoked
  1784.         // for T1.T2, for example, this function only checks whether or not T1.T2
  1785.         // is accessible from "this" type. It does not recheck whether or not T1 is
  1786.         // accessible.
  1787.         //
  1788.         if (this_type -> outermost_type != type -> outermost_type)
  1789.         {
  1790.             if (type -> ACC_PRIVATE())
  1791.                  ReportTypeInaccessible(ast, type);
  1792.             else if (type -> ACC_PROTECTED())
  1793.             {
  1794.                 //
  1795.                 // TODO: we have filed a query to Sun regarding which test is required here!
  1796.                 //
  1797.                 // if (! (type -> ContainingPackage() == this_package || this_type -> IsSubclass(type)))
  1798.                 //
  1799.                 if (! (type -> ContainingPackage() == this_package || this_type -> HasProtectedAccessTo(type)))
  1800.                     ReportTypeInaccessible(ast, type);
  1801.             }
  1802.             else if (! (type -> ACC_PUBLIC() || type -> ContainingPackage() == this_package))
  1803.                  ReportTypeInaccessible(ast, type);
  1804.         }
  1805.     }
  1806.  
  1807.     return;
  1808. }
  1809.  
  1810.  
  1811. void Semantic::TypeNestAccessCheck(AstExpression *name)
  1812. {
  1813.     AstSimpleName *simple_name = name -> SimpleNameCast();
  1814.     AstFieldAccess *field_access = name -> FieldAccessCast();
  1815.  
  1816.     assert(simple_name || field_access);
  1817.  
  1818.     if (field_access)
  1819.         TypeNestAccessCheck(field_access -> base);
  1820.  
  1821.     TypeSymbol *type = (simple_name ? simple_name -> Type() : field_access -> Type());
  1822.     if (type)
  1823.         TypeAccessCheck(name, type);
  1824.  
  1825.     return;
  1826. }
  1827.  
  1828.  
  1829. void Semantic::ConstructorAccessCheck(AstClassInstanceCreationExpression *class_creation, MethodSymbol *constructor)
  1830. {
  1831.     TypeSymbol *this_type = ThisType();
  1832.     TypeSymbol *containing_type = constructor -> containing_type;
  1833.  
  1834.     if (this_type -> outermost_type != containing_type -> outermost_type)
  1835.     {
  1836.         if (constructor -> ACC_PRIVATE())
  1837.         {
  1838.             ReportSemError(SemanticError::PRIVATE_CONSTRUCTOR_NOT_ACCESSIBLE,
  1839.                            class_creation -> class_type -> LeftToken(),
  1840.                            class_creation -> right_parenthesis_token,
  1841.                            constructor -> Header(),
  1842.                            containing_type -> ContainingPackage() -> PackageName(),
  1843.                            containing_type -> ExternalName());
  1844.         }
  1845.         else if (! class_creation -> symbol -> TypeCast() -> Anonymous())
  1846.         {
  1847.             if (constructor -> ACC_PROTECTED())
  1848.             {
  1849.                 //
  1850.                 // TODO: we need to file a query to Sun regarding which test is required here!
  1851.                 // According to the rules 6.6.2 in the JLS, access to a protected constructor
  1852.                 // that is not contained in the same package is only valid through a "super(...)" call.
  1853.                 // However, javac seems to have relaxed this restriction to allow subclass access...
  1854.                 //
  1855.                 //
  1856.                 // TODO: we have filed a query to Sun regarding which test is required here!
  1857.                 //
  1858.                 // if (! (containing_type -> ContainingPackage() == this_package || this_type -> IsSubclass(containing_type)))
  1859.                 //
  1860.                 if (! (containing_type -> ContainingPackage() == this_package || this_type -> HasProtectedAccessTo(containing_type)))
  1861.                 {
  1862.                     ReportSemError(SemanticError::PROTECTED_CONSTRUCTOR_NOT_ACCESSIBLE,
  1863.                                    class_creation -> class_type -> LeftToken(),
  1864.                                    class_creation -> right_parenthesis_token,
  1865.                                    constructor -> Header(),
  1866.                                    containing_type -> ContainingPackage() -> PackageName(),
  1867.                                    containing_type -> ExternalName());
  1868.                 }
  1869.             }
  1870.             else if (! (constructor -> ACC_PUBLIC() || containing_type -> ContainingPackage() == this_package))
  1871.             {
  1872.                 ReportSemError(SemanticError::DEFAULT_CONSTRUCTOR_NOT_ACCESSIBLE,
  1873.                                class_creation -> class_type -> LeftToken(),
  1874.                                class_creation -> right_parenthesis_token,
  1875.                                constructor -> Header(),
  1876.                                containing_type -> ContainingPackage() -> PackageName(),
  1877.                                containing_type -> ExternalName());
  1878.             }
  1879.         }
  1880.     }
  1881.  
  1882.     return;
  1883. }
  1884.  
  1885.  
  1886. void Semantic::MemberAccessCheck(AstFieldAccess *field_access, TypeSymbol *base_type, Symbol *symbol)
  1887. {
  1888.     TypeSymbol *this_type = ThisType();
  1889.  
  1890.     VariableSymbol *variable_symbol = symbol -> VariableCast();
  1891.     MethodSymbol *method_symbol = symbol -> MethodCast();
  1892.  
  1893.     assert(variable_symbol || method_symbol);
  1894.  
  1895.     AccessFlags *flags = (variable_symbol ? (AccessFlags *) variable_symbol : (AccessFlags *) method_symbol);
  1896.     TypeSymbol *containing_type = (variable_symbol ? variable_symbol -> owner -> TypeCast() : method_symbol -> containing_type);
  1897.  
  1898.     assert(containing_type);
  1899.  
  1900.     AstExpression *base = field_access -> base;
  1901.  
  1902.     //
  1903.     // When this function, MemberAccessCheck is invoked, it is assumed that the function TypeAccessCheck
  1904.     // has already been invoked as follows:
  1905.     //
  1906.     //    TypeAccessCheck(base, base_type);
  1907.     //
  1908.     // and that the check below has already been performed.
  1909.     //
  1910.     //    if (! (base_type -> ACC_PUBLIC() || base_type -> ContainingPackage() == this_package))
  1911.     //        ReportTypeInaccessible(base, base_type);
  1912.     //
  1913.  
  1914.     if (this_type -> outermost_type != containing_type -> outermost_type)
  1915.     {
  1916.         if (flags -> ACC_PRIVATE())
  1917.         {
  1918.             ReportSemError((variable_symbol ? SemanticError::PRIVATE_FIELD_NOT_ACCESSIBLE
  1919.                                             : SemanticError::PRIVATE_METHOD_NOT_ACCESSIBLE),
  1920.                            field_access -> identifier_token,
  1921.                            field_access -> identifier_token,
  1922.                            lex_stream -> NameString(field_access -> identifier_token),
  1923.                            containing_type -> ContainingPackage() -> PackageName(),
  1924.                            containing_type -> ExternalName());
  1925.         }
  1926.         else if (flags -> ACC_PROTECTED())
  1927.         {
  1928.             //
  1929.             // TODO: This whole area is very Murky!!! This "feature" is not valid
  1930.             // according to the language spec. However, its use is so widespread
  1931.             // that we decided to accept it while issuing a strong "Caution" message
  1932.             // to encourage the user to not use it.
  1933.             //
  1934.             if (flags -> ACC_STATIC() && this_type -> IsSubclass(containing_type))
  1935.             {
  1936.                 ReportSemError((variable_symbol ? SemanticError::STATIC_PROTECTED_FIELD_ACCESS
  1937.                                                 : SemanticError::STATIC_PROTECTED_METHOD_ACCESS),
  1938.                                field_access -> LeftToken(),
  1939.                                field_access -> RightToken(),
  1940.                                lex_stream -> NameString(field_access -> identifier_token),
  1941.                                containing_type -> ContainingPackage() -> PackageName(),
  1942.                                containing_type -> ExternalName());
  1943.             }else
  1944.  
  1945.             //
  1946.             // TODO: This whole area is very Murky!!! This is the only test that is required
  1947.             // according to the language spec and the inner classes document.
  1948.             //
  1949.             if (! (base -> IsSuperExpression() ||
  1950.                    containing_type -> ContainingPackage() == this_package ||
  1951.                    (this_type -> HasProtectedAccessTo(containing_type) &&
  1952.                     (base_type -> IsSubclass(this_type) || base_type -> IsOwner(this_type)))))
  1953.             {
  1954.                 ReportSemError((variable_symbol ? SemanticError::PROTECTED_FIELD_NOT_ACCESSIBLE
  1955.                                                 : SemanticError::PROTECTED_METHOD_NOT_ACCESSIBLE),
  1956.                                field_access -> identifier_token,
  1957.                                field_access -> identifier_token,
  1958.                                lex_stream -> NameString(field_access -> identifier_token),
  1959.                                containing_type -> ContainingPackage() -> PackageName(),
  1960.                                containing_type -> ExternalName());
  1961.             }
  1962.         }
  1963.         else if (! (flags -> ACC_PUBLIC() || containing_type -> ContainingPackage() == this_package))
  1964.         {
  1965.             ReportSemError((variable_symbol ? SemanticError::DEFAULT_FIELD_NOT_ACCESSIBLE
  1966.                                             : SemanticError::DEFAULT_METHOD_NOT_ACCESSIBLE),
  1967.                            field_access -> identifier_token,
  1968.                            field_access -> identifier_token,
  1969.                            lex_stream -> NameString(field_access -> identifier_token),
  1970.                            containing_type -> ContainingPackage() -> PackageName(),
  1971.                            containing_type -> ExternalName());
  1972.         }
  1973.     }
  1974.  
  1975.     return;
  1976. }
  1977.  
  1978.  
  1979. void Semantic::SimpleNameAccessCheck(AstSimpleName *simple_name, TypeSymbol *containing_type, Symbol *symbol)
  1980. {
  1981.     TypeSymbol *this_type = ThisType();
  1982.  
  1983.     VariableSymbol *variable_symbol = symbol -> VariableCast();
  1984.     MethodSymbol *method_symbol = symbol -> MethodCast();
  1985.  
  1986.     assert(variable_symbol || method_symbol);
  1987.  
  1988.     AccessFlags *flags = (variable_symbol ? (AccessFlags *) variable_symbol : (AccessFlags *) method_symbol);
  1989.  
  1990.     if (! (containing_type -> ACC_PUBLIC() || this_type -> ContainingPackage() == containing_type -> ContainingPackage()))
  1991.         ReportTypeInaccessible(simple_name, containing_type);
  1992.  
  1993.     if (this_type -> outermost_type != containing_type -> outermost_type)
  1994.     {
  1995.         if (flags -> ACC_PRIVATE())
  1996.         {
  1997.             ReportSemError((variable_symbol ? SemanticError::PRIVATE_FIELD_NOT_ACCESSIBLE
  1998.                                             : SemanticError::PRIVATE_METHOD_NOT_ACCESSIBLE),
  1999.                            simple_name -> identifier_token,
  2000.                            simple_name -> identifier_token,
  2001.                            lex_stream -> NameString(simple_name -> identifier_token),
  2002.                            containing_type -> ContainingPackage() -> PackageName(),
  2003.                            containing_type -> ExternalName());
  2004.         }
  2005.         else if (flags -> ACC_PROTECTED())
  2006.         {
  2007.             if (! (containing_type -> ContainingPackage() == this_package || this_type -> IsSubclass(containing_type)))
  2008.             {
  2009.                 ReportSemError((variable_symbol ? SemanticError::PROTECTED_FIELD_NOT_ACCESSIBLE
  2010.                                                 : SemanticError::PROTECTED_METHOD_NOT_ACCESSIBLE),
  2011.                                simple_name -> identifier_token,
  2012.                                simple_name -> identifier_token,
  2013.                                lex_stream -> NameString(simple_name -> identifier_token),
  2014.                                containing_type -> ContainingPackage() -> PackageName(),
  2015.                                containing_type -> ExternalName());
  2016.             }
  2017.         }
  2018.         else if (! (flags -> ACC_PUBLIC() || containing_type -> ContainingPackage() == this_package))
  2019.         {
  2020.             ReportSemError((variable_symbol ? SemanticError::DEFAULT_FIELD_NOT_ACCESSIBLE
  2021.                                             : SemanticError::DEFAULT_METHOD_NOT_ACCESSIBLE),
  2022.                            simple_name -> identifier_token,
  2023.                            simple_name -> identifier_token,
  2024.                            lex_stream -> NameString(simple_name -> identifier_token),
  2025.                            containing_type -> ContainingPackage() -> PackageName(),
  2026.                            containing_type -> ExternalName());
  2027.         }
  2028.     }
  2029.  
  2030.  
  2031.  
  2032.     return;
  2033. }
  2034.  
  2035.  
  2036. void Semantic::FindVariableMember(TypeSymbol *type, TypeSymbol *environment_type, AstFieldAccess *field_access)
  2037. {
  2038.     TypeSymbol *this_type = ThisType();
  2039.  
  2040.     //
  2041.     // This operation may throw NullPointerException
  2042.     //
  2043.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  2044.     if (exception_set)
  2045.     {
  2046.         exception_set -> AddElement(control.RuntimeException());
  2047.         exception_set -> AddElement(control.Error());
  2048.     }
  2049.  
  2050.     if (type -> Bad())
  2051.     {
  2052.         //
  2053.         // If no error has been detected so far, report this as an error so that
  2054.         // we don't try to generate code later. On the other hand, if an error
  2055.         // had been detected prior to this, don't flood the user with spurious
  2056.         // messages.
  2057.         //
  2058.         if (NumErrors() == 0)
  2059.             ReportAccessedFieldNotFound(field_access, type);
  2060.         field_access -> symbol = control.no_type;
  2061.     }
  2062.     else if (type == control.null_type || type -> Primitive())
  2063.     {
  2064.         ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  2065.                        field_access -> base -> LeftToken(),
  2066.                        field_access -> base -> RightToken(),
  2067.                        type -> Name());
  2068.         field_access -> symbol = control.no_type;
  2069.     }
  2070.     else
  2071.     {
  2072.         TypeAccessCheck(field_access -> base, type);
  2073.  
  2074.         VariableSymbol *variable_symbol = FindVariableInType(type, field_access);
  2075.         if (variable_symbol)
  2076.         {
  2077.             assert(variable_symbol -> IsTyped());
  2078.  
  2079.             //
  2080.             // If a variable is FINAL and initialized with a constant expression,
  2081.             // we substitute the expression here. JLS section 15.27, pp 381-382.
  2082.             //
  2083.             // TODO: Note that the JLS is a bit ambiguous and that a strict reading
  2084.             // of 15.27 would prohibit substitution of a final constant value here.
  2085.             // However, javac seems to accept it and as the section on qualified name
  2086.             // only mentions "final" but not "static" and as it is not possible to derefence
  2087.             // a non-static final with a TypeName, we decided to relax the rules also.
  2088.             //
  2089.             if (variable_symbol -> ACC_FINAL() && field_access -> base -> IsName())
  2090.             {
  2091.                 //
  2092.                 // If the field declaration of the type has been completely processed,
  2093.                 // simply retrieve the value. Otherwise, compute the value of the
  2094.                 // initialization expression in question on the fly if the variable
  2095.                 // in question is not in the same type. Recall that static variables
  2096.                 // must be processed in the textual order in which they appear in the
  2097.                 // body of a type. Therefore, if the static initialization of a field
  2098.                 // refers to another variable in the same type it must have appeared
  2099.                 // before the current field declaration otherwise we will emit an error
  2100.                 // message later...
  2101.                 //
  2102.                 if (variable_symbol -> IsDeclarationComplete())
  2103.                     field_access -> value = variable_symbol -> initial_value;
  2104.                 else if (variable_symbol -> declarator)
  2105.                 {
  2106.                     AstVariableDeclarator *declarator = variable_symbol -> declarator -> VariableDeclaratorCast();
  2107.                     //
  2108.                     // If the variable declarator in question exists and its computation is not
  2109.                     // pending (to avoid looping) and it has a simple expression initializer.
  2110.                     //
  2111.                     if (declarator && (! declarator -> pending) &&
  2112.                         declarator -> variable_initializer_opt &&
  2113.                         (! declarator -> variable_initializer_opt -> ArrayInitializerCast()))
  2114.                     {
  2115.                         TypeSymbol *variable_type = (TypeSymbol *) variable_symbol -> owner;
  2116.                         Semantic *sem = variable_type -> semantic_environment -> sem;
  2117.                         field_access -> value = sem -> ComputeFinalValue(declarator);
  2118.                     }
  2119.                 }
  2120.             }
  2121.  
  2122.             TypeSymbol *containing_type = (TypeSymbol *) variable_symbol -> owner;
  2123.             //
  2124.             // Access to an private or protected variable in an enclosing type ?
  2125.             //
  2126.             if (this_type -> outermost_type == environment_type -> outermost_type &&
  2127.                 (variable_symbol -> ACC_PRIVATE() && this_type != containing_type))
  2128.             {
  2129.                 if (field_access -> IsConstant())
  2130.                     field_access -> symbol = variable_symbol;
  2131.                 else
  2132.                 {
  2133.                     AstFieldAccess *method_name = compilation_unit -> ast_pool -> GenFieldAccess();
  2134.                     method_name -> base = field_access -> base; // TODO: WARNING: sharing of Ast subtree !!!
  2135.                     method_name -> dot_token = field_access -> identifier_token;
  2136.                     method_name -> identifier_token = field_access -> identifier_token;
  2137.                     method_name -> symbol = variable_symbol;
  2138.  
  2139.                     AstMethodInvocation *p       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2140.                     p -> method                  = method_name;
  2141.                     p -> left_parenthesis_token  = field_access -> identifier_token;
  2142.                     p -> right_parenthesis_token = field_access -> identifier_token;
  2143.                     p -> symbol                  = TypeSymbol::GetReadAccessMethod(variable_symbol);
  2144.  
  2145.                     if (! variable_symbol -> ACC_STATIC())
  2146.                         p -> AddArgument(field_access -> base);
  2147.  
  2148.                     field_access -> resolution_opt = p;
  2149.                     field_access -> symbol = p -> symbol;
  2150.                 }
  2151.             }
  2152.             else
  2153.             {
  2154.                 field_access -> symbol = variable_symbol;
  2155.                 MemberAccessCheck(field_access, type, variable_symbol);
  2156.             }
  2157.         }
  2158.         else
  2159.         {
  2160.             TypeSymbol *inner_type;
  2161.             if (inner_type = FindNestedType(type, field_access -> identifier_token))
  2162.                  ReportSemError(SemanticError::FIELD_IS_TYPE,
  2163.                                 field_access -> identifier_token,
  2164.                                 field_access -> identifier_token,
  2165.                                 lex_stream -> NameString(field_access -> identifier_token));
  2166.             else ReportAccessedFieldNotFound(field_access, type);
  2167.  
  2168.             field_access -> symbol = control.no_type;
  2169.         }
  2170.     }
  2171.  
  2172.     return;
  2173. }
  2174.  
  2175. //
  2176. // NOTE that method names are not processed here but by the function
  2177. // ProcessMethodName.
  2178. //
  2179. void Semantic::ProcessAmbiguousName(Ast *name)
  2180. {
  2181.     TypeSymbol *this_type = ThisType();
  2182.  
  2183.     AstSimpleName *simple_name;
  2184.  
  2185.     //
  2186.     // ...If the ambiguous name is a simple name,...
  2187.     //
  2188.     if (simple_name = name -> SimpleNameCast())
  2189.     {
  2190.         TypeSymbol *type;
  2191.         //
  2192.         // ... If the Identifier appears within the scope (6.3) if a local variable declaration (14.3)
  2193.         // or parameter declaration (8.4.1, 8.6.1, 14.18) with that name, then the ambiguous name is
  2194.         // reclassified as an ExpressionName...
  2195.         //
  2196.         // ...Otherwise, consider the class or interface C within whose declaration the Identifier occurs.
  2197.         // If C has one or more fields with that name, which may be either declared within it or inherited,
  2198.         // then the Ambiguous name is reclassified as an ExpressionName....
  2199.         //
  2200.         SemanticEnvironment *where_found;
  2201.         VariableSymbol *variable_symbol = FindVariableInEnvironment(where_found, state_stack.Top(), simple_name -> identifier_token);
  2202.         if (variable_symbol)
  2203.         {
  2204.             assert(variable_symbol -> IsTyped());
  2205.  
  2206.             //
  2207.             // A variable_symbol that is FINAL may have an initial value.
  2208.             // If variable_symbol is not final then its initial value is NULL.
  2209.             //
  2210.             simple_name -> value = variable_symbol -> initial_value;
  2211.             simple_name -> symbol = variable_symbol;
  2212.  
  2213.             CheckSimpleName(simple_name, where_found);
  2214.  
  2215.             //
  2216.             // If the variable belongs to an outer type, add the proper
  2217.             // pointer dereferences (and method access in the case of a
  2218.             // private variable) necessary to  get to it.
  2219.             //
  2220.             if (where_found != state_stack.Top())
  2221.                 CreateAccessToScopedVariable(simple_name, where_found -> Type());
  2222.         }
  2223.         //
  2224.         // ...Otherwise, if a type of that name is declared in the compilation unit (7.3) containing
  2225.         // the Identifier, either by a single-type-import declaration (7.5.1) or by a class or interface
  2226.         // type declaration (7.6), then the Ambiguous name is reclassified as a TypeName...
  2227.         //
  2228.         // ...Otherwise, if a type of that name is declared in another compilation unit (7.3) of the
  2229.         // package (7.1) of the compilation unit containing the Identifier, then the Ambiguous Name
  2230.         // is reclassified as a TypeName...
  2231.         //
  2232.         // ...Otherwise, if a type of that name is declared by exactly one type-import-on-demand declaration
  2233.         // (7.5.2) of the compilation unit containing the Identifier, then the AmbiguousName is reclassified
  2234.         // as a TypeName
  2235.         //
  2236.         // ...Otherwise, if a type of that name is declared by more than one type-import-on-demand declaration
  2237.         // of the compilation unit containing the Identifier, then a compile-time error results.
  2238.         //
  2239.         else if (type = FindType(simple_name -> identifier_token))
  2240.              simple_name -> symbol = type;
  2241.         //
  2242.         // ...Otherwise, the Ambiguous name is reclassified as a PackageName. A later step determines
  2243.         // whether or not a package of that name actually exists.
  2244.         //
  2245.         else
  2246.         {
  2247.             NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  2248.             PackageSymbol *package = control.external_table.FindPackageSymbol(name_symbol);
  2249.             if (package)
  2250.                 simple_name -> symbol = package;
  2251.             else
  2252.             {
  2253.                 package = control.external_table.InsertPackageSymbol(name_symbol, NULL);
  2254.                 control.FindPathsToDirectory(package);
  2255.                 simple_name -> symbol = package;
  2256.             }
  2257.         }
  2258.     }
  2259.     //
  2260.     // ...If the ambiguous name is a qualified name,...
  2261.     //
  2262.     else
  2263.     {
  2264.         AstFieldAccess *field_access = (AstFieldAccess *) name;
  2265.  
  2266.         assert(name -> FieldAccessCast());
  2267.  
  2268.         TypeSymbol *type = NULL;
  2269.  
  2270.         if (field_access -> IsClassAccess())
  2271.         {
  2272.             if (! control.option.one_one)
  2273.             {
  2274.                 ReportSemError(SemanticError::ONE_ONE_FEATURE,
  2275.                                field_access -> LeftToken(),
  2276.                                field_access -> RightToken());
  2277.             }
  2278.  
  2279.             AddDependence(this_type, control.NoClassDefFoundError(), field_access -> identifier_token);
  2280.             AddDependence(this_type, control.ClassNotFoundException(), field_access -> identifier_token);
  2281.  
  2282.             AstTypeExpression *base = (AstTypeExpression *) field_access -> base;
  2283.  
  2284.             AstArrayType *array_type = base -> type -> ArrayTypeCast();
  2285.             if (array_type)
  2286.             {
  2287.                 AstPrimitiveType *primitive_type = array_type -> type -> PrimitiveTypeCast();
  2288.                 TypeSymbol *unit = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(array_type -> type));
  2289.                 type = unit -> GetArrayType((Semantic *) this, array_type -> NumBrackets());
  2290.             }
  2291.             else
  2292.             {
  2293.                 AstPrimitiveType *primitive_type = base -> type -> PrimitiveTypeCast();
  2294.                 type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(base -> type));
  2295.             }
  2296.  
  2297.             TypeSymbol *outermost_type = this_type -> outermost_type;
  2298.  
  2299.             if (type -> Primitive())
  2300.             {
  2301.                 if (type == control.int_type)
  2302.                      type = control.Integer();
  2303.                 else if (type == control.double_type)
  2304.                      type = control.Double();
  2305.                 else if (type == control.char_type)
  2306.                      type = control.Character();
  2307.                 else if (type == control.long_type)
  2308.                      type = control.Long();
  2309.                 else if (type == control.float_type)
  2310.                      type = control.Float();
  2311.                 else if (type == control.byte_type)
  2312.                      type = control.Byte();
  2313.                 else if (type == control.short_type)
  2314.                      type = control.Short();
  2315.                 else if (type == control.boolean_type)
  2316.                      type = control.Boolean();
  2317.                 else // (type == control.void_type)
  2318.                      type = control.Void();
  2319.                 base -> symbol = type;
  2320.  
  2321.                 VariableSymbol *variable_symbol = type -> FindVariableSymbol(control.type_name_symbol);
  2322.  
  2323.                 assert(variable_symbol);
  2324.  
  2325.                 if (control.option.deprecation &&
  2326.                     variable_symbol -> IsDeprecated() &&
  2327.                     variable_symbol -> owner -> TypeCast() -> outermost_type != ThisType() -> outermost_type)
  2328.                 {
  2329.                     ReportSemError(SemanticError::DEPRECATED_FIELD,
  2330.                                    field_access -> identifier_token,
  2331.                                    field_access -> identifier_token,
  2332.                                    variable_symbol -> Name(),
  2333.                                    variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  2334.                                    variable_symbol -> owner -> TypeCast() -> ExternalName());
  2335.                 }
  2336.  
  2337.                 if (! variable_symbol -> IsTyped())
  2338.                     variable_symbol -> ProcessVariableSignature((Semantic *) this, field_access -> identifier_token);
  2339.  
  2340.                 field_access -> symbol = variable_symbol;
  2341.             }
  2342.             else
  2343.             {
  2344.                 TypeAccessCheck(base, array_type ? type -> base_type : type);
  2345.                 base -> symbol = type;
  2346.  
  2347.                 if (outermost_type -> ACC_INTERFACE())
  2348.                 {
  2349.                     TypeSymbol *class_literal_type = outermost_type -> FindOrInsertClassLiteralClass(field_access -> identifier_token);
  2350.                     AddDependence(this_type, class_literal_type, field_access -> identifier_token);
  2351.  
  2352.                     AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(field_access -> identifier_token);
  2353.                     simple_name -> symbol = class_literal_type;
  2354.  
  2355.                     AstFieldAccess *method_access = compilation_unit -> ast_pool -> GenFieldAccess();
  2356.                     method_access -> base = simple_name;
  2357.                     method_access -> dot_token = field_access -> identifier_token;
  2358.                     method_access -> identifier_token = field_access -> identifier_token;
  2359.  
  2360.                     AstStringLiteral *string_literal = compilation_unit -> ast_pool -> GenStringLiteral(field_access -> identifier_token);
  2361.                     string_literal -> value = type -> FindOrInsertClassLiteralName(control);
  2362.                     string_literal -> symbol = control.String();
  2363.  
  2364.                     AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2365.                     method_call -> method                  = method_access;
  2366.                     method_call -> left_parenthesis_token  = field_access -> identifier_token;
  2367.                     method_call -> AddArgument(string_literal);
  2368.                     method_call -> right_parenthesis_token = field_access -> identifier_token;
  2369.                     method_call -> symbol                  = class_literal_type -> ClassLiteralMethod();
  2370.  
  2371.                     field_access -> resolution_opt = method_call;
  2372.                     field_access -> symbol = (method_call -> symbol ? method_call -> symbol : control.no_type);
  2373.                 }
  2374.                 else
  2375.                 {
  2376.                     AddDependence(this_type, control.Class(), field_access -> identifier_token);
  2377.  
  2378.                     VariableSymbol *variable_symbol = outermost_type -> FindOrInsertClassLiteral(type);
  2379.                     if (this_type == outermost_type)
  2380.                     {
  2381.                         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(field_access -> identifier_token);
  2382.                         simple_name -> symbol = variable_symbol;
  2383.  
  2384.                         field_access -> symbol = variable_symbol;
  2385.                         field_access -> resolution_opt = simple_name;
  2386.                     }
  2387.                     else
  2388.                     {
  2389.                         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(field_access -> identifier_token);
  2390.                         simple_name -> symbol      = outermost_type;
  2391.  
  2392.                         AstFieldAccess *method_access     = compilation_unit -> ast_pool -> GenFieldAccess();
  2393.                         method_access -> base             = simple_name;
  2394.                         method_access -> dot_token        = field_access -> identifier_token;
  2395.                         method_access -> identifier_token = field_access -> identifier_token;
  2396.                         method_access -> symbol           = variable_symbol; // the variable in question
  2397.  
  2398.                         //
  2399.                         // Recall that the variable is static...
  2400.                         //
  2401.                         assert(variable_symbol -> ACC_STATIC());
  2402.  
  2403.                         AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2404.                         method_call -> method                  = method_access;
  2405.                         method_call -> left_parenthesis_token  = field_access -> identifier_token;
  2406.                         method_call -> right_parenthesis_token = field_access -> identifier_token;
  2407.                         method_call -> symbol                  = TypeSymbol::GetWriteAccessMethod(variable_symbol);
  2408.  
  2409.                         field_access -> resolution_opt = method_call;
  2410.                         field_access -> symbol = TypeSymbol::GetReadAccessMethod(variable_symbol);
  2411.                     }
  2412.                 }
  2413.             }
  2414.         }
  2415.         else
  2416.         {
  2417.             AstExpression* base = field_access -> base;
  2418.             AstFieldAccess *sub_field_access = base -> FieldAccessCast();
  2419.             simple_name = base -> SimpleNameCast();
  2420.  
  2421.             //
  2422.             // ...First, classify the name or expression to the left of the '.'...
  2423.             //
  2424.             if (simple_name || sub_field_access)
  2425.                  ProcessAmbiguousName(base);
  2426.             else ProcessExpression(base);
  2427.  
  2428.             if (base -> symbol == control.no_type)
  2429.             {
  2430.                 field_access -> symbol = control.no_type;
  2431.                 return;
  2432.             }
  2433.  
  2434.             wchar_t *identifier_name = lex_stream -> NameString(field_access -> identifier_token);
  2435.             PackageSymbol *package;
  2436.  
  2437.             Symbol *symbol = base -> symbol;
  2438.  
  2439.             if (field_access -> IsThisAccess() || field_access -> IsSuperAccess())
  2440.             {
  2441.                 if (! control.option.one_one)
  2442.                 {
  2443.                     ReportSemError(SemanticError::ONE_ONE_FEATURE,
  2444.                                    field_access -> LeftToken(),
  2445.                                    field_access -> RightToken());
  2446.                 }
  2447.  
  2448.                 TypeSymbol *enclosing_type = symbol -> TypeCast();
  2449.                 if (enclosing_type == control.no_type)
  2450.                     field_access -> symbol = control.no_type;
  2451.                 else
  2452.                 {
  2453.                     if (! enclosing_type)
  2454.                     {
  2455.                         ReportSemError(SemanticError::NOT_A_TYPE,
  2456.                                        field_access -> base -> LeftToken(),
  2457.                                        field_access -> base -> RightToken());
  2458.                         field_access -> symbol = control.no_type;
  2459.                     }
  2460.                     else if (enclosing_type -> ACC_INTERFACE())
  2461.                     {
  2462.                         ReportSemError(SemanticError::NOT_A_CLASS,
  2463.                                        field_access -> base -> LeftToken(),
  2464.                                        field_access -> base -> RightToken(),
  2465.                                        enclosing_type -> ContainingPackage() -> PackageName(),
  2466.                                        enclosing_type -> ExternalName());
  2467.                         field_access -> symbol = control.no_type;
  2468.                     }
  2469.                     else
  2470.                     {
  2471.                         if (! (this_type -> IsNestedIn(enclosing_type) && this_type -> CanAccess(enclosing_type)))
  2472.                         {
  2473.                             if (this_type == enclosing_type && field_access -> IsThisAccess())
  2474.                             {
  2475.                                 ReportSemError(SemanticError::MISPLACED_THIS_EXPRESSION,
  2476.                                                field_access -> LeftToken(),
  2477.                                                field_access -> RightToken());
  2478.                             }
  2479.                             else
  2480.                             {
  2481.                                 ReportSemError(SemanticError::ILLEGAL_THIS_FIELD_ACCESS,
  2482.                                                field_access -> LeftToken(),
  2483.                                                field_access -> RightToken(),
  2484.                                                enclosing_type -> ContainingPackage() -> PackageName(),
  2485.                                                enclosing_type -> ExternalName(),
  2486.                                                this_type -> ContainingPackage() -> PackageName(),
  2487.                                                this_type -> ExternalName());
  2488.                             }
  2489.  
  2490.                             field_access -> symbol = control.no_type;
  2491.                         }
  2492.                         else
  2493.                         {
  2494.                             //
  2495.                             // Note that in the case of a Super access, there will be further resolution later.
  2496.                             //
  2497.                             field_access -> resolution_opt = CreateAccessToType(field_access, enclosing_type);
  2498.                             field_access -> symbol = field_access -> resolution_opt -> symbol;
  2499.                         }
  2500.                     }
  2501.                 }
  2502.             }
  2503.             else if (package = symbol -> PackageCast())
  2504.             {
  2505.                 //
  2506.                 // ... If there is a package whose name is the name to the left of the '.' and that package
  2507.                 // contains a declaration of a type whose name is the same as the Identifier, then the
  2508.                 // AmbiguousName is reclassified as a TypeName...
  2509.                 //
  2510.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2511.                 type = package -> FindTypeSymbol(name_symbol);
  2512.  
  2513.                 if (type)
  2514.                 {
  2515.                     if (type -> SourcePending())
  2516.                         control.ProcessHeaders(type -> file_symbol);
  2517.                     field_access -> symbol = type;
  2518.                 }
  2519.                 else
  2520.                 {
  2521.                     FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  2522.                     if (file_symbol)
  2523.                     {
  2524.                         type = ReadType(file_symbol, package, name_symbol, field_access -> identifier_token);
  2525.                         field_access -> symbol = type;
  2526.                     }
  2527.                     //
  2528.                     // ... Otherwise, this AmbiguousName is reclassified as a PackageName. A later step determines
  2529.                     // whether or not a package of that name actually exists...
  2530.                     //
  2531.                     else
  2532.                     {
  2533.                         PackageSymbol *subpackage = package -> FindPackageSymbol(name_symbol);
  2534.                         if (! subpackage) // A new package ?
  2535.                         {
  2536.                             subpackage = package -> InsertPackageSymbol(name_symbol);
  2537.                             control.FindPathsToDirectory(subpackage);
  2538.                         }
  2539.                         field_access -> symbol = subpackage;
  2540.                     }
  2541.                 }
  2542.             }
  2543.             else if (sub_field_access && sub_field_access -> IsSuperAccess())
  2544.             {
  2545.                 if (sub_field_access -> Type() == control.no_type)
  2546.                     field_access -> symbol = control.no_type;
  2547.                 else if (sub_field_access -> Type() == control.Object())
  2548.                 {
  2549.                     ReportSemError(SemanticError::OBJECT_HAS_NO_SUPER_TYPE,
  2550.                                    sub_field_access -> LeftToken(),
  2551.                                    sub_field_access -> RightToken(),
  2552.                                    sub_field_access -> Type() -> ContainingPackage() -> PackageName(),
  2553.                                    sub_field_access -> Type() -> ExternalName());
  2554.                     field_access -> symbol = control.no_type;
  2555.                 }
  2556.                 else
  2557.                 {
  2558.                     type = sub_field_access -> Type() -> super;
  2559.                     FindVariableMember(type, sub_field_access -> Type(), field_access);
  2560.                 }
  2561.             }
  2562.             //
  2563.             // ...If the name to the left of the '.' is reclassified as a TypeName, then this AmbiguousName is
  2564.             // reclassified as an ExpressionName
  2565.             //
  2566.             else if (type = symbol -> TypeCast())
  2567.             {
  2568.                 if (type -> Bad())
  2569.                 {
  2570.                     //
  2571.                     // If no error has been detected so far, report this as an error so that
  2572.                     // we don't try to generate code later. On the other hand, if an error
  2573.                     // had been detected prior to this, don't flood the user with spurious
  2574.                     // messages.
  2575.                     //
  2576.                     if (NumErrors() == 0)
  2577.                         ReportAccessedFieldNotFound(field_access, type);
  2578.                     field_access -> symbol = control.no_type;
  2579.                 }
  2580.                 else if (type == control.null_type || type -> Primitive())
  2581.                 {
  2582.                     ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  2583.                                    base -> LeftToken(),
  2584.                                    base -> RightToken(),
  2585.                                    type -> Name());
  2586.                     field_access -> symbol = control.no_type;
  2587.                 }
  2588.                 else
  2589.                 {
  2590.                     TypeAccessCheck(base, type);
  2591.  
  2592.                     VariableSymbol *variable_symbol = FindVariableInType(type, field_access);
  2593.  
  2594.                     if (variable_symbol)
  2595.                     {
  2596.                         assert(variable_symbol -> IsTyped());
  2597.  
  2598.                         if (base -> IsName()) // a type name (as opposed to an expression) ?
  2599.                         {
  2600.                             if (variable_symbol -> ACC_STATIC())
  2601.                             {
  2602.                                 //
  2603.                                 // A variable_symbol that is STATIC and FINAL must have an initial value.
  2604.                                 // If it is dereferenced by a type name, then associate the value with the
  2605.                                 // subexpression to identify it as a constant subexpression.
  2606.                                 //
  2607.                                 if (variable_symbol -> ACC_FINAL())
  2608.                                 {
  2609.                                     //
  2610.                                     // If the field declaration of the type has been completely processed,
  2611.                                     // simply retrieve the value. Otherwise, compute the value of the
  2612.                                     // initialization expression in question on the fly if the variable
  2613.                                     // in question is not in the same type. Recall that static variables
  2614.                                     // must be processed in the textual order in which they appear in the
  2615.                                     // body of a type. Therefore, if the static initialization of a field
  2616.                                     // refers to another variable in the same type it must have appeared
  2617.                                     // before the current field declaration otherwise we will emit an error
  2618.                                     // message later...
  2619.                                     //
  2620.                                     if (variable_symbol -> IsDeclarationComplete())
  2621.                                         field_access -> value = variable_symbol -> initial_value;
  2622.                                     else if (variable_symbol -> declarator)
  2623.                                     {
  2624.                                         AstVariableDeclarator *declarator = variable_symbol -> declarator -> VariableDeclaratorCast();
  2625.                                         //
  2626.                                         // If the variable declarator in question exists and its computation is not
  2627.                                         // pending (to avoid looping) and it has a simple expression initializer.
  2628.                                         //
  2629.                                         if (declarator &&
  2630.                                             (! declarator -> pending) &&
  2631.                                             declarator -> variable_initializer_opt &&
  2632.                                             (! declarator -> variable_initializer_opt -> ArrayInitializerCast()))
  2633.                                         {
  2634.                                             TypeSymbol *type = (TypeSymbol *) variable_symbol -> owner;
  2635.                                             Semantic *sem = type -> semantic_environment -> sem;
  2636.                                             field_access -> value = sem -> ComputeFinalValue(declarator);
  2637.                                         }
  2638.                                     }
  2639.                                 }
  2640.                             }
  2641.                             else
  2642.                             {
  2643.                                 ReportSemError(SemanticError::NAME_NOT_CLASS_VARIABLE,
  2644.                                                field_access -> identifier_token,
  2645.                                                field_access -> identifier_token,
  2646.                                                identifier_name);
  2647.                             }
  2648.                         }
  2649.  
  2650.                         TypeSymbol *containing_type = (TypeSymbol *) variable_symbol -> owner;
  2651.                         //
  2652.                         // Access to a private or protected variable in an enclosing type ?
  2653.                         //
  2654.                         if (this_type -> outermost_type == type -> outermost_type &&
  2655.                             (variable_symbol -> ACC_PRIVATE() && this_type != containing_type))
  2656.                         {
  2657.                             if (field_access -> IsConstant())
  2658.                                 field_access -> symbol = variable_symbol;
  2659.                             else
  2660.                             {
  2661.                                 AstFieldAccess *method_name     = compilation_unit -> ast_pool -> GenFieldAccess();
  2662.                                 method_name -> base             = field_access -> base;  // TODO: WARNING: sharing of Ast subtree !!!
  2663.                                 method_name -> dot_token        = field_access -> identifier_token;
  2664.                                 method_name -> identifier_token = field_access -> identifier_token;
  2665.                                 method_name -> symbol           = variable_symbol; // the variable in question
  2666.  
  2667.                                 //
  2668.                                 // variable_symbol is static.
  2669.                                 //
  2670.                                 AstMethodInvocation *p       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2671.                                 p -> method                  = method_name;
  2672.                                 p -> left_parenthesis_token  = field_access -> identifier_token;
  2673.                                 p -> right_parenthesis_token = field_access -> identifier_token;
  2674.                                 p -> symbol                  = TypeSymbol::GetReadAccessMethod(variable_symbol);
  2675.  
  2676.                                 if (! variable_symbol -> ACC_STATIC())
  2677.                                     p -> AddArgument(field_access -> base); // TODO: WARNING: sharing of Ast subtree !!!
  2678.  
  2679.                                 field_access -> resolution_opt = p;
  2680.                                 field_access -> symbol = p -> symbol;
  2681.                             }
  2682.                         }
  2683.                         else
  2684.                         {
  2685.                             field_access -> symbol = variable_symbol;
  2686.                             MemberAccessCheck(field_access, type, variable_symbol);
  2687.                         }
  2688.                     }
  2689.                     else
  2690.                     {
  2691.                         TypeSymbol *inner_type = FindNestedType(type, field_access -> identifier_token);
  2692.                         if (inner_type)
  2693.                         {
  2694.                             field_access -> symbol = inner_type;
  2695.                             TypeAccessCheck(field_access, inner_type);
  2696.                         }
  2697.                         else
  2698.                         {
  2699.                             ReportAccessedFieldNotFound(field_access, type);
  2700.                             field_access -> symbol = control.no_type;
  2701.                         }
  2702.                     }
  2703.                 }
  2704.             }
  2705.             //
  2706.             // ...If the name to the left of the '.' is reclassified as an ExpressionName, then this
  2707.             // AmbiguousName is reclassified as an instance member field reference. Note that we have two subcases to
  2708.             // consider: the case where the subexpression to the left is resolved to a variable and
  2709.             // the case where the subexpression is resolved to a method call.
  2710.             //
  2711.             else if (symbol -> VariableCast())
  2712.             {
  2713.                 assert(symbol -> VariableCast() -> IsTyped());
  2714.  
  2715.                 type = symbol -> VariableCast() -> Type();
  2716.  
  2717.                 FindVariableMember(type, type, field_access);
  2718.             }
  2719.             else if (symbol -> MethodCast())
  2720.             {
  2721.                 assert(symbol -> MethodCast() -> IsTyped());
  2722.  
  2723.                 type = symbol -> MethodCast() -> Type();
  2724.  
  2725.                 FindVariableMember(type, type, field_access);
  2726.             }
  2727.             else // illegal Name !!!
  2728.             {
  2729.                 ReportSemError(SemanticError::UNKNOWN_QUALIFIED_NAME_BASE,
  2730.                                base -> LeftToken(),
  2731.                                base -> RightToken(),
  2732.                                symbol -> Name());
  2733.                 field_access -> symbol = control.no_type;
  2734.             }
  2735.         }
  2736.  
  2737.         if (type)
  2738.         {
  2739.             TypeSymbol *parent_type = (type -> IsArray() ? type -> base_type : type);
  2740.             if (! parent_type -> Primitive())
  2741.                 AddDependence(this_type, parent_type, field_access -> identifier_token, field_access -> IsConstant());
  2742.         }
  2743.     }
  2744.  
  2745.     return;
  2746. }
  2747.  
  2748.  
  2749. void Semantic::ProcessFieldAccess(Ast *expr)
  2750. {
  2751.     AstFieldAccess *field_access = (AstFieldAccess *) expr;
  2752.  
  2753.     ProcessAmbiguousName(field_access);
  2754.  
  2755.     if (field_access -> symbol != control.no_type)
  2756.     {
  2757.         PackageSymbol *package;
  2758.         TypeSymbol *type;
  2759.  
  2760.         if (package = field_access -> symbol -> PackageCast())
  2761.         {
  2762.             ReportSemError(SemanticError::UNKNOWN_AMBIGUOUS_NAME,
  2763.                            field_access -> LeftToken(),
  2764.                            field_access -> RightToken(),
  2765.                            package -> PackageName());
  2766.             field_access -> symbol = control.no_type;
  2767.         }
  2768.         else if ((type = field_access -> symbol -> TypeCast()) && (! field_access -> IsThisAccess()))
  2769.         {
  2770.             ReportSemError(SemanticError::TYPE_NOT_FIELD,
  2771.                            field_access -> LeftToken(),
  2772.                            field_access -> RightToken(),
  2773.                            type -> Name());
  2774.             field_access -> symbol = control.no_type;
  2775.         }
  2776.         else // Assertion: either it's not a variable (an error) or the signature of the variable has been typed
  2777.         {
  2778.             assert((! field_access -> symbol -> VariableCast()) || field_access -> symbol -> VariableCast() -> IsTyped());
  2779.         }
  2780.     }
  2781.  
  2782.     return;
  2783. }
  2784.  
  2785.  
  2786. void Semantic::ProcessCharacterLiteral(Ast *expr)
  2787. {
  2788.     AstCharacterLiteral *char_literal = (AstCharacterLiteral *) expr;
  2789.  
  2790.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(char_literal -> character_literal_token);
  2791.  
  2792.     if (! literal -> value)
  2793.         control.int_pool.FindOrInsertChar(literal);
  2794.     if (literal -> value == &control.bad_value)
  2795.     {
  2796.         ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  2797.                        char_literal -> LeftToken(),
  2798.                        char_literal -> RightToken());
  2799.         char_literal -> symbol = control.no_type;
  2800.     }
  2801.     else
  2802.     {
  2803.         char_literal -> value = literal -> value;
  2804.         char_literal -> symbol = control.char_type;
  2805.     }
  2806. }
  2807.  
  2808.  
  2809. void Semantic::ProcessIntegerLiteral(Ast *expr)
  2810. {
  2811.     AstIntegerLiteral *int_literal = (AstIntegerLiteral *) expr;
  2812.  
  2813.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(int_literal -> integer_literal_token);
  2814.  
  2815.     if (! literal -> value)
  2816.         control.int_pool.FindOrInsertInt(literal);
  2817.     if (literal -> value == &control.bad_value)
  2818.     {
  2819.         ReportSemError(SemanticError::INVALID_INT_VALUE,
  2820.                        int_literal -> LeftToken(),
  2821.                        int_literal -> RightToken());
  2822.         int_literal -> symbol = control.no_type;
  2823.     }
  2824.     else
  2825.     {
  2826.         int_literal -> value = literal -> value;
  2827.         int_literal -> symbol = control.int_type;
  2828.     }
  2829. }
  2830.  
  2831.  
  2832. void Semantic::ProcessLongLiteral(Ast *expr)
  2833. {
  2834.     AstLongLiteral *long_literal = (AstLongLiteral *) expr;
  2835.  
  2836.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(long_literal -> long_literal_token);
  2837.  
  2838.     if (! literal -> value)
  2839.         control.long_pool.FindOrInsertLong(literal);
  2840.     if (literal -> value == &control.bad_value)
  2841.     {
  2842.         ReportSemError(SemanticError::INVALID_LONG_VALUE,
  2843.                        long_literal -> LeftToken(),
  2844.                        long_literal -> RightToken());
  2845.         long_literal -> symbol = control.no_type;
  2846.     }
  2847.     else
  2848.     {
  2849.         long_literal -> value = literal -> value;
  2850.         long_literal -> symbol = control.long_type;
  2851.     }
  2852. }
  2853.  
  2854.  
  2855. void Semantic::ProcessFloatingPointLiteral(Ast *expr)
  2856. {
  2857.     AstFloatingPointLiteral *float_literal = (AstFloatingPointLiteral *) expr;
  2858.  
  2859.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(float_literal -> floating_point_literal_token);
  2860.  
  2861.     if (! literal -> value)
  2862.         control.float_pool.FindOrInsertFloat(literal);
  2863.     if (literal -> value == &control.bad_value)
  2864.     {
  2865.         ReportSemError(SemanticError::INVALID_FLOAT_VALUE,
  2866.                        float_literal -> LeftToken(),
  2867.                        float_literal -> RightToken());
  2868.         float_literal -> symbol = control.no_type;
  2869.     }
  2870.     else
  2871.     {
  2872.         float_literal -> value = literal -> value;
  2873.         float_literal -> symbol = control.float_type;
  2874.     }
  2875. }
  2876.  
  2877.  
  2878. void Semantic::ProcessDoubleLiteral(Ast *expr)
  2879. {
  2880.     AstDoubleLiteral *double_literal = (AstDoubleLiteral *) expr;
  2881.  
  2882.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(double_literal -> double_literal_token);
  2883.  
  2884.     if (! literal -> value)
  2885.         control.double_pool.FindOrInsertDouble(literal);
  2886.     if (literal -> value == &control.bad_value)
  2887.     {
  2888.         ReportSemError(SemanticError::INVALID_DOUBLE_VALUE,
  2889.                        double_literal -> LeftToken(),
  2890.                        double_literal -> RightToken());
  2891.         double_literal -> symbol = control.no_type;
  2892.     }
  2893.     else
  2894.     {
  2895.         double_literal -> value = literal -> value;
  2896.         double_literal -> symbol = control.double_type;
  2897.     }
  2898. }
  2899.  
  2900.  
  2901. void Semantic::ProcessTrueLiteral(Ast *expr)
  2902. {
  2903.     AstExpression *true_literal = (AstTrueLiteral *) expr;
  2904.  
  2905.     true_literal -> value = control.int_pool.FindOrInsert((int) 1);
  2906.     true_literal -> symbol = control.boolean_type;
  2907. }
  2908.  
  2909.  
  2910. void Semantic::ProcessFalseLiteral(Ast *expr)
  2911. {
  2912.     AstExpression *false_literal = (AstFalseLiteral *) expr;
  2913.  
  2914.     false_literal -> value = control.int_pool.FindOrInsert((int) 0);
  2915.     false_literal -> symbol = control.boolean_type;
  2916. }
  2917.  
  2918.  
  2919. void Semantic::ProcessStringLiteral(Ast *expr)
  2920. {
  2921.     AstStringLiteral *string_literal = (AstStringLiteral *) expr;
  2922.  
  2923.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(string_literal -> string_literal_token);
  2924.  
  2925.     if (! literal -> value)
  2926.         control.Utf8_pool.FindOrInsertString(literal);
  2927.     if (literal -> value == &control.bad_value)
  2928.     {
  2929.         ReportSemError(SemanticError::INVALID_STRING_VALUE,
  2930.                        string_literal -> LeftToken(),
  2931.                        string_literal -> RightToken());
  2932.         string_literal -> symbol = control.no_type;
  2933.     }
  2934.     else
  2935.     {
  2936.         string_literal -> value = literal -> value;
  2937.         string_literal -> symbol = control.String();
  2938.     }
  2939. }
  2940.  
  2941.  
  2942. void Semantic::ProcessArrayAccess(Ast *expr)
  2943. {
  2944.     AstArrayAccess *array_access = (AstArrayAccess *) expr;
  2945.  
  2946.     //
  2947.     // This operation may throw NullPointerException or IndefOutOfBoundsException
  2948.     //
  2949.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  2950.     if (exception_set)
  2951.     {
  2952.         exception_set -> AddElement(control.RuntimeException());
  2953.         exception_set -> AddElement(control.Error());
  2954.     }
  2955.  
  2956.     ProcessExpression(array_access -> base);
  2957.     ProcessExpression(array_access -> expression);
  2958.     array_access -> expression = PromoteUnaryNumericExpression(array_access -> expression);
  2959.     if (array_access -> expression -> Type() != control.int_type && array_access -> expression -> symbol != control.no_type)
  2960.     {
  2961.         ReportSemError(SemanticError::TYPE_NOT_INTEGER,
  2962.                        array_access -> expression -> LeftToken(),
  2963.                        array_access -> expression -> RightToken(),
  2964.                        array_access -> expression -> Type() -> Name());
  2965.     }
  2966.  
  2967.     TypeSymbol *array_type = array_access -> base -> Type();
  2968.     if (array_type -> IsArray())
  2969.         array_access -> symbol = array_type -> ArraySubtype();
  2970.     else
  2971.     {
  2972.         if (array_type != control.no_type)
  2973.             ReportSemError(SemanticError::TYPE_NOT_ARRAY,
  2974.                            array_access -> base -> LeftToken(),
  2975.                            array_access -> base -> RightToken(),
  2976.                            array_access -> base -> Type() -> Name());
  2977.         array_access -> symbol = control.no_type;
  2978.     }
  2979.  
  2980.     return;
  2981. }
  2982.  
  2983.  
  2984. MethodSymbol *Semantic::FindMethodMember(TypeSymbol *type, TypeSymbol *environment_type, AstMethodInvocation *method_call)
  2985. {
  2986.     MethodSymbol *method = NULL;
  2987.     AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  2988.  
  2989.     if (type -> Bad())
  2990.     {
  2991.         //
  2992.         // If no error has been detected so far, report this as an error so that
  2993.         // we don't try to generate code later. On the other hand, if an error
  2994.         // had been detected prior to this, don't flood the user with spurious
  2995.         // messages.
  2996.         //
  2997.         if (NumErrors() == 0)
  2998.             ReportMethodNotFound(method_call, lex_stream -> NameString(field_access -> identifier_token));
  2999.         method_call -> symbol = control.no_type;
  3000.     }
  3001.     else if (type == control.null_type || type -> Primitive())
  3002.     {
  3003.         ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  3004.                        field_access -> base -> LeftToken(),
  3005.                        field_access -> base -> RightToken(),
  3006.                        type -> Name());
  3007.         method_call -> symbol = control.no_type;
  3008.     }
  3009.     else
  3010.     {
  3011.         TypeSymbol *this_type = ThisType();
  3012.         TypeAccessCheck(field_access -> base, type);
  3013.  
  3014.         method = FindMethodInType(type, method_call);
  3015.  
  3016.         if (method)
  3017.         {
  3018.             assert(method -> IsTyped());
  3019.  
  3020.             //
  3021.             // Access to an private or protected method in an enclosing type ?
  3022.             //
  3023.             if (this_type -> outermost_type == environment_type -> outermost_type &&
  3024.                 (method -> ACC_PRIVATE() && this_type != method -> containing_type))
  3025.             {
  3026.                 if (method -> ACC_STATIC())
  3027.                     method_call -> symbol = TypeSymbol::GetReadAccessMethod(method);
  3028.                 else
  3029.                 {
  3030.                     AstMethodInvocation *old_method_call = method_call;
  3031.  
  3032.                     //
  3033.                     // TODO: WARNING: sharing of subtrees...
  3034.                     //
  3035.                     AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  3036.                     method_call -> method                  = old_method_call -> method;
  3037.                     method_call -> left_parenthesis_token  = old_method_call -> left_parenthesis_token;
  3038.                     method_call -> right_parenthesis_token = old_method_call -> right_parenthesis_token;
  3039.                     method_call -> symbol                  = TypeSymbol::GetReadAccessMethod(method);
  3040.                     method_call -> AddArgument(field_access -> base);
  3041.                     for (int i = 0; i < old_method_call -> NumArguments(); i++)
  3042.                         method_call -> AddArgument(old_method_call -> Argument(i));
  3043.  
  3044.                     old_method_call -> symbol = method;
  3045.                     old_method_call -> resolution_opt = method_call;
  3046.                 }
  3047.             }
  3048.             else
  3049.             {
  3050.                 method_call -> symbol = method;
  3051.                 MemberAccessCheck(field_access, type, method);
  3052.             }
  3053.         }
  3054.         else method_call -> symbol = control.no_type;
  3055.     }
  3056.  
  3057.     return method;
  3058. }
  3059.  
  3060.  
  3061. void Semantic::ProcessMethodName(AstMethodInvocation *method_call)
  3062. {
  3063.     TypeSymbol *this_type = ThisType();
  3064.  
  3065.     //
  3066.     // This operation may throw:
  3067.     //
  3068.     //        OutOfMemoryError
  3069.     //        NoSuchMethodError
  3070.     //        IllegalAccessError
  3071.     //        IncompatibleClassChangeError
  3072.     //
  3073.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  3074.     if (exception_set)
  3075.     {
  3076.         exception_set -> AddElement(control.RuntimeException());
  3077.         exception_set -> AddElement(control.Error());
  3078.     }
  3079.  
  3080.     AstSimpleName *simple_name;
  3081.  
  3082.     if (simple_name = method_call -> method -> SimpleNameCast())
  3083.     {
  3084.         SemanticEnvironment *where_found;
  3085.         MethodSymbol *method = FindMethodInEnvironment(where_found, state_stack.Top(), method_call);
  3086.  
  3087.         if (! method)
  3088.             method_call -> symbol = control.no_type;
  3089.         else
  3090.         {
  3091.             assert(method -> IsTyped());
  3092.  
  3093.             if (! method -> ACC_STATIC())
  3094.             {
  3095.                 //
  3096.                 // We are in a static region if we are:
  3097.                 //     . in the body of a static method
  3098.                 //     . in the body of a static initializer
  3099.                 //     . precessing an initializer expression for a static variable.
  3100.                 //
  3101.                 // See StaticRegion() Semantic.h for more detail.
  3102.                 //
  3103.                 // Note that a constructor is never static.
  3104.                 //
  3105.                 if (StaticRegion())
  3106.                 {
  3107.                     ReportSemError(SemanticError::METHOD_NOT_CLASS_METHOD,
  3108.                                    simple_name -> identifier_token,
  3109.                                    method_call -> right_parenthesis_token,
  3110.                                    lex_stream -> NameString(simple_name -> identifier_token));
  3111.                 }
  3112.                 else if (ExplicitConstructorInvocation())
  3113.                 {
  3114.                     if (this_type -> IsSubclass(method -> containing_type))
  3115.                     {
  3116.                         //
  3117.                         // If the method in question is an instance method
  3118.                         // that is declared in this_type (this_type is definitely
  3119.                         // a class) or one of its super classes, then we have an error ->
  3120.                         //
  3121.                         ReportSemError(SemanticError::INSTANCE_METHOD_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3122.                                        method_call -> LeftToken(),
  3123.                                        method_call -> RightToken(),
  3124.                                        method -> Header(),
  3125.                                        method -> containing_type -> Name());
  3126.                     }
  3127.                 }
  3128.             }
  3129.  
  3130.             method_call -> symbol = method;
  3131.  
  3132.             //
  3133.             // If the method is a private method belonging to an outer type,
  3134.             // give the ast simple_name access to its read_method.
  3135.             //
  3136.             if (where_found != state_stack.Top())
  3137.                 CreateAccessToScopedMethod(method_call, where_found -> Type());
  3138.         }
  3139.     }
  3140.     else
  3141.     {
  3142.         AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  3143.         AstExpression* base = field_access -> base;
  3144.         AstFieldAccess *sub_field_access = base -> FieldAccessCast();
  3145.  
  3146.         if (base -> SimpleNameCast() || sub_field_access)
  3147.              ProcessAmbiguousName(base);
  3148.         else ProcessExpression(base);
  3149.  
  3150.         if (base -> symbol == control.no_type)
  3151.         {
  3152.             method_call -> symbol = control.no_type;
  3153.             return;
  3154.         }
  3155.  
  3156.         TypeSymbol *type = NULL;
  3157.         Symbol *symbol = base -> symbol;
  3158.  
  3159.         //
  3160.         // If the base is a "super" field access, resolve it before proceeding
  3161.         //
  3162.         if (sub_field_access && sub_field_access -> IsSuperAccess())
  3163.         {
  3164.             if (sub_field_access -> Type() == control.no_type)
  3165.                 method_call -> symbol = control.no_type;
  3166.             else if (sub_field_access -> Type() == control.Object())
  3167.             {
  3168.                 ReportSemError(SemanticError::OBJECT_HAS_NO_SUPER_TYPE,
  3169.                                sub_field_access -> LeftToken(),
  3170.                                sub_field_access -> RightToken(),
  3171.                                sub_field_access -> Type() -> ContainingPackage() -> PackageName(),
  3172.                                sub_field_access -> Type() -> ExternalName());
  3173.                 method_call -> symbol = control.no_type;
  3174.             }
  3175.             else
  3176.             {
  3177.                 MethodSymbol *method = FindMethodMember(sub_field_access -> Type() -> super, sub_field_access -> Type(), method_call);
  3178.  
  3179.                 //
  3180.                 // TODO: This test was added in order to pass the test in section 8.4.3.1, page 159.
  3181.                 //       All I can find in the spec is that one example. Nowhere else could I find a
  3182.                 //       more formal statement.
  3183.                 //
  3184.                 if (method && method -> ACC_ABSTRACT())
  3185.                 {
  3186.                     ReportSemError(SemanticError::ABSTRACT_METHOD_INVOCATION,
  3187.                                    field_access -> LeftToken(),
  3188.                                    field_access -> identifier_token,
  3189.                                    lex_stream -> NameString(field_access -> identifier_token));
  3190.                 }
  3191.             }
  3192.         }
  3193.         else if (type = symbol -> TypeCast())
  3194.         {
  3195.             if (type -> Bad())
  3196.             {
  3197.                 //
  3198.                 // If no error has been detected so far, report this as an error so that
  3199.                 // we don't try to generate code later. On the other hand, if an error
  3200.                 // had been detected prior to this, don't flood the user with spurious
  3201.                 // messages.
  3202.                 //
  3203.                 if (NumErrors() == 0)
  3204.                     ReportMethodNotFound(method_call, lex_stream -> NameString(field_access -> identifier_token));
  3205.                 method_call -> symbol = control.no_type;
  3206.             }
  3207.             else if (type == control.null_type || type -> Primitive())
  3208.             {
  3209.                 ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  3210.                                base -> LeftToken(),
  3211.                                base -> RightToken(),
  3212.                                type -> Name());
  3213.                 method_call -> symbol = control.no_type;
  3214.             }
  3215.             else
  3216.             {
  3217.                 TypeAccessCheck(base, type);
  3218.  
  3219.                 MethodSymbol *method = FindMethodInType(type, method_call);
  3220.  
  3221.                 if (method)
  3222.                 {
  3223.                     assert(method -> IsTyped());
  3224.  
  3225.                     if (base -> IsName() && (! method -> ACC_STATIC()))
  3226.                     {
  3227.                         ReportSemError(SemanticError::METHOD_NOT_CLASS_METHOD,
  3228.                                        field_access -> LeftToken(),
  3229.                                        field_access -> identifier_token,
  3230.                                        lex_stream -> NameString(field_access -> identifier_token));
  3231.                     }
  3232.                     //
  3233.                     // TODO: This test was added in order to pass the test in section 8.4.3.1, page 159.
  3234.                     //       All I can find in the spec is that one example. Nowhere else could I find a
  3235.                     //       more formal statement.
  3236.                     //
  3237.                     else if (base -> IsSuperExpression() && method -> ACC_ABSTRACT())
  3238.                         ReportSemError(SemanticError::ABSTRACT_METHOD_INVOCATION,
  3239.                                        field_access -> LeftToken(),
  3240.                                        field_access -> identifier_token,
  3241.                                        lex_stream -> NameString(field_access -> identifier_token));
  3242.  
  3243.                     //
  3244.                     // Access to an private or protected method in an enclosing type ?
  3245.                     //
  3246.                     if (this_type -> outermost_type == type -> outermost_type &&
  3247.                         (method -> ACC_PRIVATE() && this_type != method -> containing_type))
  3248.                     {
  3249.                         if (method -> ACC_STATIC())
  3250.                             method_call -> symbol = TypeSymbol::GetReadAccessMethod(method);
  3251.                         else
  3252.                         {
  3253.                             AstMethodInvocation *old_method_call = method_call;
  3254.  
  3255.                             //
  3256.                             // TODO: WARNING: sharing of subtrees...
  3257.                             //
  3258.                             AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  3259.                             method_call -> method                  = old_method_call -> method;
  3260.                             method_call -> left_parenthesis_token  = old_method_call -> left_parenthesis_token;
  3261.                             method_call -> right_parenthesis_token = old_method_call -> right_parenthesis_token;
  3262.                             method_call -> symbol                  = TypeSymbol::GetReadAccessMethod(method);
  3263.                             method_call -> AddArgument(field_access -> base);
  3264.                             for (int i = 0; i < old_method_call -> NumArguments(); i++)
  3265.                                 method_call -> AddArgument(old_method_call -> Argument(i));
  3266.  
  3267.                             old_method_call -> symbol = method;
  3268.                             old_method_call -> resolution_opt = method_call;
  3269.                         }
  3270.                     }
  3271.                     else
  3272.                     {
  3273.                         method_call -> symbol = method;
  3274.                         MemberAccessCheck(field_access, type, method);
  3275.                     }
  3276.                 }
  3277.                 else method_call -> symbol = control.no_type;
  3278.             }
  3279.         }
  3280.         //
  3281.         // ...If the name to the left of the '.' is reclassified as an ExpressionName, then this
  3282.         // method call is reclassified as an instance member method call. Note that we have two subcases to
  3283.         // consider: the case where the subexpression to the left is resolved to a variable and
  3284.         // the case where the subexpression is resolved to a method call.
  3285.         //
  3286.         else if (symbol -> VariableCast())
  3287.         {
  3288.             assert(symbol -> VariableCast() -> IsTyped());
  3289.  
  3290.             type = symbol -> VariableCast() -> Type();
  3291.  
  3292.             (void) FindMethodMember(type, type, method_call);
  3293.         }
  3294.         else if (symbol -> MethodCast())
  3295.         {
  3296.             assert(symbol -> MethodCast() -> IsTyped());
  3297.  
  3298.             type = symbol -> MethodCast() -> Type();
  3299.  
  3300.             (void) FindMethodMember(type, type, method_call);
  3301.         }
  3302.         else // illegal Name !!!
  3303.         {
  3304.             PackageSymbol *package = symbol -> PackageCast();
  3305.             NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  3306.  
  3307.             if (package && (package -> FindTypeSymbol(name_symbol) || Control::GetFile(control, package, name_symbol)))
  3308.             {
  3309.                 ReportSemError(SemanticError::TYPE_NOT_METHOD,
  3310.                                field_access -> identifier_token,
  3311.                                field_access -> identifier_token,
  3312.                                name_symbol -> Name());
  3313.             }
  3314.             else
  3315.             {
  3316.                 ReportSemError(SemanticError::UNKNOWN_QUALIFIED_NAME_BASE,
  3317.                                field_access -> base -> LeftToken(),
  3318.                                field_access -> base -> RightToken(),
  3319.                                symbol -> Name());
  3320.             }
  3321.             method_call -> symbol = control.no_type;
  3322.         }
  3323.  
  3324.         if (type)
  3325.         {
  3326.             TypeSymbol *parent_type = (type -> IsArray() ? type -> base_type : type);
  3327.             if (! parent_type -> Primitive())
  3328.                 AddDependence(this_type, parent_type, field_access -> identifier_token);
  3329.         }
  3330.     }
  3331.  
  3332.     if (method_call -> symbol != control.no_type)
  3333.     {
  3334.         MethodSymbol *method = (MethodSymbol *) method_call -> symbol;
  3335.  
  3336.         if (exception_set)
  3337.         {
  3338.             for (int i = method -> NumThrows() - 1; i >= 0; i--)
  3339.                 exception_set -> AddElement(method -> Throws(i));
  3340. // TODO: what to do?
  3341. //            if (! method -> ACC_STATIC())
  3342. //                exception_set -> AddElement(NullPointerException());
  3343. //            if (method -> ACC_NATIVE())
  3344. //                exception_set -> AddElement(control.UnsatisfiedLinkError());
  3345.         }
  3346.  
  3347.         for (int i = 0; i < method_call -> NumArguments(); i++)
  3348.         {
  3349.             AstExpression *expr = method_call -> Argument(i);
  3350.             if (expr -> Type() != method -> FormalParameter(i) -> Type())
  3351.                 method_call -> Argument(i) = ConvertToType(expr, method -> FormalParameter(i) -> Type());
  3352.         }
  3353.  
  3354.         //
  3355.         // Recall that an instance initializer in the body of an anonymous type can
  3356.         // throw any exception. The test below allows us to skip such blocks.
  3357.         //
  3358.         if (! (this_type -> Anonymous() && ThisMethod() && ThisMethod() -> Identity() == control.block_init_name_symbol))
  3359.         {
  3360.             for (int k = method -> NumThrows() - 1; k >= 0; k--)
  3361.             {
  3362.                 TypeSymbol *exception = method -> Throws(k);
  3363.                 if (! CatchableException(exception))
  3364.                 {
  3365.                     ReportSemError(SemanticError::UNCATCHABLE_METHOD_THROWN_CHECKED_EXCEPTION,
  3366.                                    method_call -> LeftToken(),
  3367.                                    method_call -> RightToken(),
  3368.                                    method -> Header(),
  3369.                                    exception -> ContainingPackage() -> PackageName(),
  3370.                                    exception -> ExternalName());
  3371.                 }
  3372.             }
  3373.         }
  3374.     }
  3375.  
  3376.     return;
  3377. }
  3378.  
  3379.  
  3380. void Semantic::ProcessMethodInvocation(Ast *expr)
  3381. {
  3382.     AstMethodInvocation *method_call = (AstMethodInvocation *) expr;
  3383.  
  3384.     bool no_bad_argument = true;
  3385.  
  3386.     for (int i = 0; i < method_call -> NumArguments(); i++)
  3387.     {
  3388.         AstExpression *expr = method_call -> Argument(i);
  3389.         ProcessExpressionOrStringConstant(expr);
  3390.         no_bad_argument = no_bad_argument && (expr -> symbol != control.no_type);
  3391.     }
  3392.  
  3393.     if (no_bad_argument)
  3394.          ProcessMethodName(method_call);
  3395.     else method_call -> symbol = control.no_type;
  3396.  
  3397.     assert(method_call -> symbol == control.no_type || ((MethodSymbol *) method_call -> symbol) -> IsTyped());
  3398.  
  3399.     return;
  3400. }
  3401.  
  3402.  
  3403. void Semantic::ProcessNullLiteral(Ast *expr)
  3404. {
  3405.     AstNullLiteral *null_literal = (AstNullLiteral *) expr;
  3406.     null_literal -> value = control.NullValue();
  3407.     null_literal -> symbol = control.null_type;
  3408.  
  3409.     return;
  3410. }
  3411.  
  3412.  
  3413. void Semantic::ProcessThisExpression(Ast *expr)
  3414. {
  3415.     AstThisExpression *this_expression = (AstThisExpression *) expr;
  3416.  
  3417.     if (StaticRegion())
  3418.     {
  3419.         ReportSemError(SemanticError::MISPLACED_THIS_EXPRESSION,
  3420.                        this_expression -> LeftToken(),
  3421.                        this_expression -> RightToken());
  3422.         this_expression -> symbol = control.no_type;
  3423.     }
  3424.     else if (ExplicitConstructorInvocation())
  3425.     {
  3426.         ReportSemError(SemanticError::THIS_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3427.                        this_expression -> LeftToken(),
  3428.                        this_expression -> RightToken(),
  3429.                        lex_stream -> NameString(this_expression -> this_token));
  3430.         this_expression -> symbol = control.no_type;
  3431.     }
  3432.     else this_expression -> symbol = ThisType();
  3433.  
  3434.     return;
  3435. }
  3436.  
  3437.  
  3438. void Semantic::ProcessSuperExpression(Ast *expr)
  3439. {
  3440.     AstSuperExpression *super_expression = (AstSuperExpression *) expr;
  3441.  
  3442.     if (StaticRegion() || ThisType() == control.Object())
  3443.     {
  3444.          ReportSemError(SemanticError::MISPLACED_SUPER_EXPRESSION,
  3445.                         super_expression -> LeftToken(),
  3446.                         super_expression -> RightToken());
  3447.          super_expression -> symbol = control.no_type;
  3448.     }
  3449.     else if (ExplicitConstructorInvocation())
  3450.     {
  3451.         ReportSemError(SemanticError::THIS_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3452.                        super_expression -> LeftToken(),
  3453.                        super_expression -> RightToken(),
  3454.                        lex_stream -> NameString(super_expression -> super_token));
  3455.          super_expression -> symbol = control.no_type;
  3456.     }
  3457.     else super_expression -> symbol = ThisType() -> super;
  3458. }
  3459.  
  3460.  
  3461. void Semantic::ProcessParenthesizedExpression(Ast *expr)
  3462. {
  3463.     AstParenthesizedExpression *parenthesized = (AstParenthesizedExpression *) expr;
  3464.  
  3465.     ProcessExpression(parenthesized -> expression);
  3466.     parenthesized -> value = parenthesized -> expression -> value;
  3467.     parenthesized -> symbol = parenthesized -> expression -> symbol;
  3468. }
  3469.  
  3470.  
  3471. void Semantic::UpdateGeneratedLocalConstructor(MethodSymbol *constructor)
  3472. {
  3473.     TypeSymbol *local_type = constructor -> containing_type;
  3474.     MethodSymbol *local_constructor = constructor -> LocalConstructor();
  3475.  
  3476.     assert(local_constructor -> IsGeneratedLocalConstructor());
  3477.  
  3478.     BlockSymbol *block_symbol = local_constructor -> block_symbol;
  3479.  
  3480.     for (int i = 0; i < constructor -> NumFormalParameters(); i++)
  3481.     {
  3482.         VariableSymbol *param = constructor -> FormalParameter(i),
  3483.                        *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  3484.  
  3485.         assert(symbol);
  3486.  
  3487.         symbol -> SetExternalIdentity(param -> ExternalIdentity()); // TODO: do we really need this ?
  3488.         symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3489.         if (control.IsDoubleWordType(symbol -> Type()))
  3490.             block_symbol -> max_variable_index++;
  3491.         local_constructor -> AddFormalParameter(symbol);
  3492.     }
  3493.  
  3494.     //
  3495.     // If we are dealing with a constructor generated for an anonymous type and
  3496.     // the super type of the anonymous type is an inner type then the generated
  3497.     // constructor accepts an additional formal parameter which is the containing
  3498.     // type of the super type, and the name of the parameter is #0.
  3499.     //
  3500.     VariableSymbol *super_this0_variable = block_symbol -> FindVariableSymbol(control.MakeParameter(0));
  3501.     if (super_this0_variable)
  3502.     {
  3503.         super_this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3504.         local_constructor -> AddFormalParameter(super_this0_variable);
  3505.     }
  3506.     local_constructor -> SetSignature(control);
  3507.  
  3508.     //
  3509.     //
  3510.     //
  3511.     AstConstructorDeclaration *constructor_declaration = (AstConstructorDeclaration *)
  3512.                                                          constructor -> method_or_constructor_declaration;
  3513.  
  3514.     assert(constructor_declaration -> ConstructorDeclarationCast());
  3515.  
  3516.     AstConstructorBlock *constructor_block = constructor_declaration -> constructor_body;
  3517.  
  3518.     if (! (constructor_block -> explicit_constructor_invocation_opt &&
  3519.            constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast()))
  3520.     {
  3521.         constructor_block -> AllocateLocalInitStatements(local_type -> NumConstructorParameters());
  3522.  
  3523.         //
  3524.         // Generate an assignment statement for each local variable parameter.
  3525.         // Note that we do not initialize the this$0 here as the real constructor
  3526.         // will do that. If the local_type is static, its constructor_parameters
  3527.         // list does not start with this$0.
  3528.         //
  3529.         for (int i = (local_type -> ACC_STATIC() ? 0 : 1); i < local_type -> NumConstructorParameters(); i++)
  3530.         {
  3531.             VariableSymbol *param = local_type -> ConstructorParameter(i),
  3532.                            *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  3533.  
  3534.             assert(symbol);
  3535.  
  3536.             AstSimpleName *lhs = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3537.             lhs -> symbol = param;
  3538.  
  3539.             AstSimpleName *rhs = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3540.             rhs -> symbol = symbol;
  3541.  
  3542.             AstAssignmentExpression *assign = compilation_unit -> ast_pool
  3543.                                                                -> GenAssignmentExpression(AstAssignmentExpression::EQUAL,
  3544.                                                                                           constructor_block -> left_brace_token);
  3545.             assign -> left_hand_side = lhs;
  3546.             assign -> expression     = rhs;
  3547.             assign -> symbol         = lhs -> Type();
  3548.  
  3549.             AstExpressionStatement *stmt = compilation_unit -> ast_pool -> GenExpressionStatement();
  3550.             stmt -> expression           = assign;
  3551.             stmt -> semicolon_token_opt  = constructor_block -> left_brace_token;
  3552.  
  3553.             stmt -> is_reachable = true;
  3554.             stmt -> can_complete_normally = true;
  3555.  
  3556.             constructor_block -> AddLocalInitStatement(stmt);
  3557.         }
  3558.     }
  3559.  
  3560.     //
  3561.     //
  3562.     //
  3563.     AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3564.     simple_name -> symbol = constructor;
  3565.  
  3566.     assert(! constructor -> IsGeneratedLocalConstructor());
  3567.  
  3568.     AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  3569.     method_call -> method                  = simple_name;
  3570.     method_call -> left_parenthesis_token  = constructor_block -> left_brace_token;
  3571.     method_call -> right_parenthesis_token = constructor_block -> left_brace_token;
  3572.     method_call -> symbol                  = simple_name -> symbol;
  3573.  
  3574.     method_call -> AllocateArguments(constructor -> NumFormalParameters() + 1);
  3575.     if (! local_type -> ACC_STATIC())
  3576.     {
  3577.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3578.         simple_name -> symbol = block_symbol -> FindVariableSymbol(control.this0_name_symbol);
  3579.  
  3580.         assert(simple_name -> symbol);
  3581.  
  3582.         method_call -> AddArgument(simple_name);
  3583.     }
  3584.  
  3585.     for (int k = 0; k < constructor -> NumFormalParameters(); k++)
  3586.     {
  3587.         VariableSymbol *param = constructor -> FormalParameter(k),
  3588.                        *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  3589.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3590.         simple_name -> symbol = symbol;
  3591.         method_call -> AddArgument(simple_name);
  3592.     }
  3593.  
  3594.     AstExpressionStatement *stmt  = compilation_unit -> ast_pool -> GenExpressionStatement();
  3595.     stmt -> expression            = method_call;
  3596.     stmt -> semicolon_token_opt   = constructor_block -> left_brace_token;
  3597.  
  3598.     stmt -> is_reachable          = true;
  3599.     stmt -> can_complete_normally = true;
  3600.  
  3601.     constructor_block -> original_constructor_invocation = stmt;
  3602.  
  3603.     return;
  3604. }
  3605.  
  3606.  
  3607. void Semantic::UpdateLocalConstructors(TypeSymbol *inner_type)
  3608. {
  3609.     if (! ThisType() -> IsLocal()) // the method containing inner_type is not itself embedded in another method
  3610.     {
  3611.         //
  3612.         // Compute the set of local_classes we need to process here - they are
  3613.         // the inner_type itself and all the classes that are embedded in its body.
  3614.         //
  3615.         Tuple<TypeSymbol *> local_classes(8);
  3616.  
  3617.         TypeSymbol *outermost_type = inner_type -> outermost_type;
  3618.         if (outermost_type -> local) // The set of local types in the outermost type is not empty?
  3619.         {
  3620.             for (TypeSymbol *local_type = (TypeSymbol *) outermost_type -> local -> FirstElement();
  3621.                              local_type;
  3622.                              local_type = (TypeSymbol *) outermost_type -> local -> NextElement())
  3623.             {
  3624.                 if (local_type -> CanAccess(inner_type))
  3625.                     local_classes.Next() = local_type;
  3626.             }
  3627.         }
  3628.  
  3629.         for (int j = 0; j < outermost_type -> num_anonymous_types(); j++)
  3630.         {
  3631.             if (outermost_type -> AnonymousType(j) -> CanAccess(inner_type))
  3632.                 local_classes.Next() = outermost_type -> AnonymousType(j);
  3633.         }
  3634.  
  3635.         //
  3636.         // We now update each type T2 containing a call to a constructor of
  3637.         // T1 to make sure that T2 has a copy of or access to all the local
  3638.         // variables required by T1.
  3639.         //
  3640.         for (int k = 0; k < local_classes.Length(); k++)
  3641.         {
  3642.             TypeSymbol *target_local_type = local_classes[k];
  3643.  
  3644.             for (int i = 0; i < target_local_type -> NumLocalConstructorCallEnvironments(); i++)
  3645.             {
  3646.                 SemanticEnvironment *env = target_local_type -> LocalConstructorCallEnvironment(i);
  3647.                 TypeSymbol *source_local_type = env -> Type();
  3648.                 if (! source_local_type -> CanAccess(target_local_type))
  3649.                 {
  3650.                     for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3651.                          j < target_local_type -> NumConstructorParameters(); j++)
  3652.                     {
  3653.                         VariableSymbol *local = target_local_type -> ConstructorParameter(j) -> accessed_local;
  3654.  
  3655.                         //
  3656.                         // If there does not exist a variable with the same identity as the local or
  3657.                         // there exists such a variable but it is not the local then make a copy of
  3658.                         // the local in the source type.
  3659.                         //
  3660.                         if (env -> symbol_table.FindVariableSymbol(local -> Identity()) != local)
  3661.                             (void) source_local_type -> FindOrInsertLocalShadow(local);
  3662.                     }
  3663.                 }
  3664.             }
  3665.         }
  3666.  
  3667.         //
  3668.         // Now update the constructor bodies to reflect the new local variable counts and mark the local_type completed.
  3669.         //
  3670.         for (int l = 0; l < local_classes.Length(); l++)
  3671.         {
  3672.             TypeSymbol *local_type = local_classes[l];
  3673.  
  3674.             AstClassDeclaration *class_declaration = local_type -> declaration -> ClassDeclarationCast();
  3675.             AstClassInstanceCreationExpression *class_creation = local_type -> declaration -> ClassInstanceCreationExpressionCast();
  3676.  
  3677.             assert(class_declaration || class_creation);
  3678.  
  3679.             AstClassBody *class_body = (class_declaration ? class_declaration -> class_body : class_creation -> class_body_opt);
  3680.  
  3681.             if (class_body -> default_constructor)
  3682.                  UpdateGeneratedLocalConstructor(class_body -> default_constructor -> constructor_symbol);
  3683.             else
  3684.             {
  3685.                 for (int i = 0; i < class_body -> NumConstructors(); i++)
  3686.                     UpdateGeneratedLocalConstructor(class_body -> Constructor(i) -> constructor_symbol);
  3687.  
  3688.                 for (int k = 0; k < local_type -> NumPrivateAccessConstructors(); k++)
  3689.                     UpdateGeneratedLocalConstructor(local_type -> PrivateAccessConstructor(k));
  3690.             }
  3691.  
  3692.             local_type -> MarkLocalClassProcessingCompleted();
  3693.         }
  3694.  
  3695.         //
  3696.         // Now update the constructor calls
  3697.         //
  3698.         for (int m = 0; m < local_classes.Length(); m++)
  3699.         {
  3700.             TypeSymbol *target_local_type = local_classes[m];
  3701.  
  3702.             assert(target_local_type -> LocalClassProcessingCompleted());
  3703.  
  3704.             for (int i = 0; i < target_local_type -> NumLocalConstructorCallEnvironments(); i++)
  3705.             {
  3706.                 Ast *call = target_local_type -> LocalConstructorCallEnvironment(i) -> ast_construct;
  3707.                 SemanticEnvironment *env = target_local_type -> LocalConstructorCallEnvironment(i);
  3708.                 TypeSymbol *source_local_type = env -> Type();
  3709.  
  3710.                 AstClassInstanceCreationExpression *class_creation;
  3711.                 AstSuperCall *super_call;
  3712.                 AstThisCall *this_call;
  3713.  
  3714.                 if (class_creation = call -> ClassInstanceCreationExpressionCast())
  3715.                 {
  3716.                     if (class_creation -> symbol != control.no_type)
  3717.                     {
  3718.                         if (source_local_type -> CanAccess(target_local_type))
  3719.                         {
  3720.                             for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3721.                                  j < target_local_type -> NumConstructorParameters(); j++)
  3722.                             {
  3723.                                 VariableSymbol *local = target_local_type -> ConstructorParameter(j) -> accessed_local;
  3724.                                 AstSimpleName *simple_name = compilation_unit -> ast_pool
  3725.                                                                               -> GenSimpleName(class_creation -> new_token);
  3726.                                 simple_name -> symbol = local;
  3727.                                 if (source_local_type != target_local_type)
  3728.                                 {
  3729.                                     state_stack.Push(source_local_type -> semantic_environment);
  3730.                                     CreateAccessToScopedVariable(simple_name, target_local_type);
  3731.                                     state_stack.Pop();
  3732.                                 }
  3733.                                 class_creation -> AddLocalArgument(simple_name);
  3734.                             }
  3735.                         }
  3736.                         else
  3737.                         {
  3738.                             for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3739.                                  j < target_local_type -> NumConstructorParameters(); j++)
  3740.                             {
  3741.                                 VariableSymbol *local = target_local_type -> ConstructorParameter(j) -> accessed_local;
  3742.  
  3743.                                 AstSimpleName *simple_name = compilation_unit -> ast_pool
  3744.                                                                               -> GenSimpleName(class_creation -> new_token);
  3745.                                 //
  3746.                                 // If there does not exist a variable with the same identity as the local or
  3747.                                 // there exists such a variable but it is not the local then make a copy of
  3748.                                 // the local in the source type.
  3749.                                 //
  3750.                                 simple_name -> symbol = (env -> symbol_table.FindVariableSymbol(local -> Identity()) == local
  3751.                                                               ? local
  3752.                                                               : source_local_type -> FindOrInsertLocalShadow(local));
  3753.  
  3754.                                 assert(simple_name -> symbol -> VariableCast());
  3755.  
  3756.                                 class_creation -> AddLocalArgument(simple_name);
  3757.                             }
  3758.                         }
  3759.  
  3760.                         MethodSymbol *constructor = (MethodSymbol *) class_creation -> class_type -> symbol;
  3761.  
  3762.                         assert(constructor);
  3763.                         assert(constructor -> MethodCast());
  3764.                         assert(! constructor -> IsGeneratedLocalConstructor());
  3765.                         assert(constructor -> LocalConstructor());
  3766.  
  3767.                         class_creation -> class_type -> symbol = constructor -> LocalConstructor();
  3768.                     }
  3769.                 }
  3770.                 else if (super_call = call -> SuperCallCast())
  3771.                 {
  3772.                     if (super_call -> symbol -> MethodCast())
  3773.                     {
  3774.                         for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3775.                              j < target_local_type -> NumConstructorParameters(); j++)
  3776.                         {
  3777.                             VariableSymbol *local = target_local_type -> ConstructorParameter(j) -> accessed_local,
  3778.                                            *local_shadow = source_local_type -> FindOrInsertLocalShadow(local);
  3779.  
  3780.                             AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(super_call -> super_token);
  3781.                             simple_name -> symbol = env -> symbol_table.FindVariableSymbol(local_shadow -> Identity());
  3782.  
  3783.                             assert(simple_name -> symbol -> VariableCast());
  3784.  
  3785.                             super_call -> AddLocalArgument(simple_name);
  3786.                         }
  3787.  
  3788.                         MethodSymbol *constructor = (MethodSymbol *) super_call -> symbol;
  3789.  
  3790.                         assert(constructor -> MethodCast() && (! constructor -> IsGeneratedLocalConstructor()));
  3791.                         assert(constructor -> LocalConstructor());
  3792.  
  3793.                         super_call -> symbol = constructor -> LocalConstructor();
  3794.                     }
  3795.                 }
  3796.                 else
  3797.                 {
  3798.                     this_call = (AstThisCall *) call;
  3799.  
  3800.                     assert(this_call -> ThisCallCast());
  3801.  
  3802.                     if (this_call -> symbol -> MethodCast())
  3803.                     {
  3804.                         for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3805.                              j < target_local_type -> NumConstructorParameters(); j++)
  3806.                         {
  3807.                             VariableSymbol *local = target_local_type -> ConstructorParameter(j);
  3808.  
  3809.                             AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(this_call -> this_token);
  3810.                             simple_name -> symbol = env -> symbol_table.FindVariableSymbol(local -> Identity());
  3811.  
  3812.                             assert(simple_name -> symbol -> VariableCast());
  3813.  
  3814.                             this_call -> AddLocalArgument(simple_name);
  3815.                         }
  3816.  
  3817.                         MethodSymbol *constructor = (MethodSymbol *) this_call -> symbol;
  3818.  
  3819.                         assert(constructor -> MethodCast() && (! constructor -> IsGeneratedLocalConstructor()));
  3820.                         assert(constructor -> LocalConstructor());
  3821.  
  3822.                         this_call -> symbol = constructor -> LocalConstructor();
  3823.                     }
  3824.                 }
  3825.             }
  3826.         }
  3827.     }
  3828.  
  3829.     return;
  3830. }
  3831.  
  3832.  
  3833. void Semantic::GetAnonymousConstructor(AstClassInstanceCreationExpression *class_creation, TypeSymbol *anonymous_type)
  3834. {
  3835.     LexStream::TokenIndex left_loc  = class_creation -> class_type -> LeftToken(),
  3836.                           right_loc = class_creation -> right_parenthesis_token;
  3837.  
  3838.     TypeSymbol *super_type = anonymous_type -> super;
  3839.     MethodSymbol *super_constructor = FindConstructor(super_type, class_creation, left_loc, right_loc);
  3840.     if (! super_constructor)
  3841.     {
  3842.         class_creation -> class_type -> symbol = control.no_type;
  3843.         return;
  3844.     }
  3845.  
  3846.     assert(super_constructor -> IsTyped());
  3847.  
  3848.     //
  3849.     // Make constructor symbol. The associated symbol table will not contain too many elements...
  3850.     //
  3851.     BlockSymbol *block_symbol = new BlockSymbol(super_constructor -> NumFormalParameters() + 3);
  3852.     block_symbol -> max_variable_index = 1; // All types need a spot for "this".
  3853.  
  3854.     MethodSymbol *constructor = anonymous_type -> InsertConstructorSymbol(control.init_name_symbol);
  3855.     constructor -> SetType(control.void_type);
  3856.     constructor -> SetContainingType(anonymous_type);
  3857.     constructor -> SetBlockSymbol(block_symbol);
  3858.     constructor -> SetACC_PUBLIC();
  3859.  
  3860.     //
  3861.     // Report error is super constructor has throws clause, but add the exceptions to the local throws
  3862.     // clause to avoid spurious errors later !!!
  3863.     //
  3864.     int num_throws = super_constructor -> NumThrows();
  3865.     if (num_throws > 0)
  3866.     {
  3867.         for (int i = 0; i < num_throws; i++)
  3868.         {
  3869.             TypeSymbol *exception = super_constructor -> Throws(i);
  3870.             ReportSemError(SemanticError::CONSTRUCTOR_DOES_NOT_THROW_SUPER_EXCEPTION,
  3871.                           class_creation -> new_token,
  3872.                           class_creation -> RightToken(),
  3873.                           StringConstant::US_EMPTY,
  3874.                           exception -> ContainingPackage() -> PackageName(),
  3875.                           exception -> ExternalName(),
  3876.                           super_constructor -> containing_type -> ContainingPackage() -> PackageName(),
  3877.                           super_constructor -> containing_type -> ExternalName());
  3878.  
  3879.             constructor -> AddThrows(exception);
  3880.         }
  3881.     }
  3882.  
  3883.     VariableSymbol *this0_variable = NULL;
  3884.     if (anonymous_type -> IsInner())
  3885.     {
  3886.         this0_variable = block_symbol -> InsertVariableSymbol(control.this0_name_symbol);
  3887.         this0_variable -> MarkSynthetic();
  3888.         this0_variable -> SetType(anonymous_type -> ContainingType());
  3889.         this0_variable -> SetOwner(constructor);
  3890.         this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3891.     }
  3892.  
  3893.     for (int j = 0; j < super_constructor -> NumFormalParameters(); j++)
  3894.     {
  3895.         VariableSymbol *param = super_constructor -> FormalParameter(j),
  3896.                        *symbol = block_symbol -> InsertVariableSymbol(param -> Identity());
  3897.         symbol -> SetType(param -> Type());
  3898.         symbol -> SetOwner(constructor);
  3899.         symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3900.         if (control.IsDoubleWordType(symbol -> Type()))
  3901.             block_symbol -> max_variable_index++;
  3902.         constructor -> AddFormalParameter(symbol);
  3903.     }
  3904.  
  3905.     //
  3906.     //
  3907.     //
  3908.     AstSuperCall *super_call              = compilation_unit -> ast_pool -> GenSuperCall();
  3909.     super_call -> base_opt                = class_creation -> base_opt; // save initial base_opt
  3910.     super_call -> dot_token_opt           = class_creation -> new_token;
  3911.     super_call -> super_token             = class_creation -> new_token;
  3912.     super_call -> left_parenthesis_token  = class_creation -> new_token;
  3913.     super_call -> right_parenthesis_token = class_creation -> new_token;
  3914.     super_call -> semicolon_token         = class_creation -> new_token;
  3915.  
  3916.     super_call -> is_reachable            = true;
  3917.     super_call -> can_complete_normally   = true;
  3918.     super_call -> symbol                  = super_constructor;
  3919.  
  3920.     //
  3921.     // If we are in a static region, the anonymous constructor does not need a this$0 argument.
  3922.     // Otherwise, a this$0 argument that points to an instance of the immediately enclosing
  3923.     // class is required.
  3924.     //
  3925.     if (anonymous_type -> ACC_STATIC())
  3926.         class_creation -> base_opt = NULL;
  3927.     else
  3928.     {
  3929.         //
  3930.         // Within an explicit constructor invocation, a class that is immediately nested
  3931.         // in the class being created is not accessible.
  3932.         //
  3933.         if (ExplicitConstructorInvocation() && anonymous_type -> ContainingType() == ThisType())
  3934.         {
  3935.             ReportSemError(SemanticError::INNER_CONSTRUCTOR_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3936.                            class_creation -> LeftToken(),
  3937.                            class_creation -> RightToken(),
  3938.                            anonymous_type -> ContainingPackage() -> PackageName(),
  3939.                            anonymous_type -> ExternalName(),
  3940.                            ThisType() -> ContainingPackage() -> PackageName(),
  3941.                            ThisType() -> ExternalName());
  3942.             class_creation -> base_opt = NULL;
  3943.         }
  3944.         else class_creation -> base_opt = CreateAccessToType(class_creation, anonymous_type -> ContainingType());
  3945.     }
  3946.  
  3947.     AstClassBody *class_body = class_creation -> class_body_opt;
  3948.  
  3949.     AstReturnStatement *return_statement = compilation_unit -> ast_pool -> GenReturnStatement();
  3950.     return_statement -> return_token     = class_body -> left_brace_token;
  3951.     return_statement -> expression_opt   = NULL;
  3952.     return_statement -> semicolon_token  = class_body -> left_brace_token;
  3953.     return_statement -> is_reachable     = true;
  3954.  
  3955.     AstBlock *block                = compilation_unit -> ast_pool -> GenBlock();
  3956.     block -> block_symbol          = constructor -> block_symbol -> InsertBlockSymbol(1); // this symbol table will be empty
  3957.     block -> left_brace_token      = class_body -> left_brace_token;
  3958.     block -> right_brace_token     = class_body -> left_brace_token;
  3959.  
  3960.     block -> is_reachable          = true;
  3961.     block -> can_complete_normally = false;
  3962.     block -> AllocateBlockStatements(1); // this block contains one statement
  3963.     block -> AddStatement(return_statement);
  3964.  
  3965.     AstConstructorBlock *constructor_block                   = compilation_unit -> ast_pool -> GenConstructorBlock();
  3966.     constructor_block -> left_brace_token                    = class_body -> left_brace_token;
  3967.     constructor_block -> explicit_constructor_invocation_opt = super_call;
  3968.     constructor_block -> block                               = block;
  3969.     constructor_block -> right_brace_token                   = class_body -> left_brace_token;
  3970.  
  3971.     AstMethodDeclarator *method_declarator       = compilation_unit -> ast_pool -> GenMethodDeclarator();
  3972.     method_declarator -> identifier_token        = left_loc;
  3973.     method_declarator -> left_parenthesis_token  = class_creation -> left_parenthesis_token;
  3974.     method_declarator -> right_parenthesis_token = right_loc;
  3975.  
  3976.     AstConstructorDeclaration *constructor_declaration  = compilation_unit -> ast_pool -> GenConstructorDeclaration();
  3977.     constructor_declaration -> constructor_declarator   = method_declarator;
  3978.     constructor_declaration -> constructor_body         = constructor_block;
  3979.  
  3980.     constructor_declaration -> constructor_symbol = constructor;
  3981.     constructor -> method_or_constructor_declaration = constructor_declaration;
  3982.  
  3983.     //
  3984.     // Note that the constructor for the anonymous type is not added to the class body here
  3985.     // beacause we've already completely compiled it and the arguments to its super call
  3986.     // do not contain "valid" SimpleName Ast expressions. It is added to the constructor
  3987.     // body later in get_anonymous_type...
  3988.     //
  3989.     // class_body -> default_constructor = constructor_declaration;
  3990.     //
  3991.     VariableSymbol *super_this0_variable = NULL;
  3992.  
  3993.     if (anonymous_type -> IsLocal())
  3994.     {
  3995.         GenerateLocalConstructor(constructor);
  3996.  
  3997.         MethodSymbol *generated_constructor = constructor -> LocalConstructor();
  3998.  
  3999.         assert(! constructor -> IsGeneratedLocalConstructor());
  4000.         assert(generated_constructor);
  4001.  
  4002.         block_symbol = generated_constructor -> block_symbol; // use the environment of the generated constructor...
  4003.  
  4004.         if (super_call -> base_opt)
  4005.         {
  4006.             //
  4007.             // Add the this$0 parameter for the super type. However, only mark it complete and
  4008.             // do not yet assign a number to it. This will be done after we know
  4009.             // how many extra "local" variable shadows are needed. See UpdateGeneratedLocalConstructor
  4010.             //
  4011.             super_this0_variable = block_symbol -> InsertVariableSymbol(control.MakeParameter(0));
  4012.             super_this0_variable -> MarkSynthetic();
  4013.             super_this0_variable -> SetType(super_call -> base_opt -> Type());
  4014.             super_this0_variable -> SetOwner(generated_constructor);
  4015.             super_this0_variable -> MarkComplete();
  4016.         }
  4017.  
  4018.         if (super_type -> IsLocal()) // a local type may use enclosed local variables?
  4019.         {
  4020.             if (super_type -> LocalClassProcessingCompleted())
  4021.             {
  4022.                 assert(super_constructor -> LocalConstructor() && (! super_constructor -> IsGeneratedLocalConstructor()));
  4023.  
  4024.                 super_call -> symbol = super_constructor -> LocalConstructor();
  4025.  
  4026.                 //
  4027.                 // TODO: Should we set the size for the super_call arguments here ???
  4028.                 //
  4029.                 for (int i = (super_type -> ACC_STATIC() ? 0 : 1); i < super_type -> NumConstructorParameters(); i++)
  4030.                 {
  4031.                     VariableSymbol *local = super_type -> ConstructorParameter(i) -> accessed_local,
  4032.                                    *local_shadow = anonymous_type -> FindOrInsertLocalShadow(local);
  4033.  
  4034.                     AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(super_call -> super_token);
  4035.                     simple_name -> symbol = block_symbol -> FindVariableSymbol(local_shadow -> Identity());
  4036.  
  4037.                     assert(simple_name -> symbol);
  4038.  
  4039.                     super_call -> AddLocalArgument(simple_name);
  4040.                 }
  4041.             }
  4042.             else // are we currently within the body of the type in question ?
  4043.                 super_type -> AddLocalConstructorCallEnvironment(GetEnvironment(super_call));
  4044.         }
  4045.     }
  4046.     else if (super_call -> base_opt)
  4047.     {
  4048.         super_this0_variable = block_symbol -> InsertVariableSymbol(control.MakeParameter(0));
  4049.         super_this0_variable -> MarkSynthetic();
  4050.         super_this0_variable -> SetType(super_call -> base_opt -> Type());
  4051.         super_this0_variable -> SetOwner(constructor);
  4052.         super_this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  4053.  
  4054.         constructor -> AddFormalParameter(super_this0_variable);
  4055.     }
  4056.  
  4057.     //
  4058.     // Complete the definition of the constructor and update the super call accordingly.
  4059.     //
  4060.     if (super_this0_variable)
  4061.     {
  4062.         class_creation -> AddArgument(super_call -> base_opt); // pass the original base expression as argument to anonymous class.
  4063.  
  4064.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(class_creation -> new_token);
  4065.         simple_name -> symbol = super_this0_variable;
  4066.         super_call -> base_opt = simple_name; // pass the base expression argument to the super class
  4067.     }
  4068.  
  4069.     constructor -> SetSignature(control, this0_variable); // we now have all the information to set the signature of the constructor.
  4070.  
  4071.     //
  4072.     // Are we guaranteed to have all the info available here? Yes,
  4073.     // because if the anonymous type is not local to a method, then its super
  4074.     // type cannot be local to a method. Therefore, no extra argument (other than
  4075.     // the proper this$0 specified in the base) is needed. If on the other hand the
  4076.     // anonymous type is local and its supertype is also local, it must have appeared
  4077.     // before the anonymous type and therefore its information has already been computed.
  4078.     //
  4079.     for (int k = 0; k < super_constructor -> NumFormalParameters(); k++)
  4080.     {
  4081.         VariableSymbol *param = super_constructor -> FormalParameter(k),
  4082.                        *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  4083.  
  4084.         assert(symbol);
  4085.  
  4086.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(class_creation -> new_token);
  4087.         simple_name -> symbol = symbol;
  4088.         super_call -> AddArgument(simple_name);
  4089.     }
  4090.  
  4091.     class_creation -> class_type -> symbol = constructor;
  4092.  
  4093.     return;
  4094. }
  4095.  
  4096.  
  4097. TypeSymbol *Semantic::GetAnonymousType(AstClassInstanceCreationExpression *class_creation, TypeSymbol *super_type)
  4098. {
  4099.     TypeSymbol *this_type = ThisType();
  4100.  
  4101.     if (super_type -> ACC_FINAL())
  4102.     {
  4103.          ReportSemError(SemanticError::SUPER_IS_FINAL,
  4104.                         class_creation -> class_type -> LeftToken(),
  4105.                         class_creation -> class_type -> RightToken(),
  4106.                         super_type -> ContainingPackage() -> PackageName(),
  4107.                         super_type -> ExternalName());
  4108.     }
  4109.  
  4110.     AstClassBody *class_body = class_creation -> class_body_opt;
  4111.     TypeSymbol *outermost_type = this_type -> outermost_type;
  4112.  
  4113.     //
  4114.     // Make up a proper name for the anonymous type
  4115.     //
  4116.     IntToWstring value(outermost_type -> num_anonymous_types() + 1);
  4117.  
  4118.     int length = value.Length() + outermost_type -> NameLength() + 1; // +1 for $
  4119.     wchar_t *anonymous_name = new wchar_t[length + 1];
  4120.     wcscpy(anonymous_name, outermost_type -> Name());
  4121.     wcscat(anonymous_name, StringConstant::US__DS);
  4122.     wcscat(anonymous_name, value.String());
  4123.  
  4124.     NameSymbol *name_symbol = control.FindOrInsertName(anonymous_name, length);
  4125.  
  4126.     assert((! ThisMethod()) || LocalSymbolTable().Top());
  4127.  
  4128.     TypeSymbol *inner_type = (ThisMethod() ? LocalSymbolTable().Top() -> InsertAnonymousTypeSymbol(name_symbol)
  4129.                                            : this_type -> InsertAnonymousTypeSymbol(name_symbol));
  4130.     inner_type -> SetACC_PRIVATE();
  4131.     inner_type -> MarkAnonymous();
  4132.     inner_type -> outermost_type = outermost_type;
  4133.     inner_type -> supertypes_closure = new SymbolSet;
  4134.     inner_type -> subtypes_closure = new SymbolSet;
  4135.     inner_type -> semantic_environment = new SemanticEnvironment((Semantic *) this, inner_type, state_stack.Top());
  4136.     inner_type -> declaration = class_creation;
  4137.     inner_type -> file_symbol = source_file_symbol;
  4138.     inner_type -> SetOwner(ThisMethod() ? (Symbol *) ThisMethod() : (Symbol *) this_type);
  4139.     //
  4140.     // Add 3 extra elements for padding. May need a default constructor and other support elements.
  4141.     //
  4142.     inner_type -> SetSymbolTable(class_body -> NumClassBodyDeclarations() + 3);
  4143.     inner_type -> SetLocation();
  4144.     inner_type -> SetSignature(control);
  4145.  
  4146.     //
  4147.     // TODO: As an anonymous type cannot be a super class, it makes sense to mark
  4148.     // is final. This allows jikes to be consistent with javac in emitting an
  4149.     // error message when the anonymous class is checked in an instanceof
  4150.     // operation against an interface. However, this fact is not documented
  4151.     // in the 1.1 document. Furthermore, the class file that is emitted for an
  4152.     // anonymous flag (when processed by javac) does not have the FINAL flag turned on.
  4153.     // We also turn this flag off after processing the body of the anonymmous type.
  4154.     // See bolow...
  4155.     //
  4156.     inner_type -> SetACC_FINAL();
  4157.  
  4158.     if (StaticRegion())
  4159.          inner_type -> SetACC_STATIC();
  4160.     else inner_type -> InsertThis(0);
  4161.  
  4162.     if (super_type -> ACC_INTERFACE())
  4163.     {
  4164.          inner_type -> AddInterface(super_type);
  4165.          inner_type -> super = control.Object();
  4166.     }
  4167.     else inner_type -> super = super_type;
  4168.  
  4169.     outermost_type -> AddAnonymousType(inner_type);
  4170.     delete [] anonymous_name;
  4171.  
  4172.     //
  4173.     //
  4174.     //
  4175.     GetAnonymousConstructor(class_creation, inner_type);
  4176.  
  4177.     //
  4178.     // Now process the body of the anonymous class !!!
  4179.     //
  4180.     CheckClassMembers(inner_type, class_body);
  4181.     ProcessNestedTypeHeaders(inner_type, class_body);
  4182.     if (inner_type -> owner -> MethodCast())
  4183.          ProcessSuperTypesOfOuterType(inner_type);
  4184.     else ProcessNestedSuperTypes(inner_type);
  4185.  
  4186.     //
  4187.     // If the class body has not yet been parsed, do so now.
  4188.     //
  4189.     if (class_body -> UnparsedClassBodyCast())
  4190.     {
  4191.         if (! control.parser -> InitializerParse(lex_stream, class_body))
  4192.              compilation_unit -> kind = Ast::BAD_COMPILATION; // mark the fact that syntax errors were detected
  4193.         else
  4194.         {
  4195.             inner_type -> MarkHeaderProcessed();
  4196.             ProcessMembers(inner_type -> semantic_environment, class_body);
  4197.             CompleteSymbolTable(inner_type -> semantic_environment, class_body -> left_brace_token, class_body);
  4198.         }
  4199.  
  4200.         if (! control.parser -> BodyParse(lex_stream, class_body))
  4201.              compilation_unit -> kind = Ast::BAD_COMPILATION; // mark the fact that syntax errors were detected
  4202.         else ProcessExecutableBodies(inner_type -> semantic_environment, class_body);
  4203.     }
  4204.     else // The relevant bodies have already been parsed
  4205.     {
  4206.         inner_type -> MarkHeaderProcessed();
  4207.         ProcessMembers(inner_type -> semantic_environment, class_body);
  4208.         CompleteSymbolTable(inner_type -> semantic_environment, class_body -> left_brace_token, class_body);
  4209.         ProcessExecutableBodies(inner_type -> semantic_environment, class_body);
  4210.     }
  4211.  
  4212.     //
  4213.     // Add the default constructor to the body of the anonymous type.
  4214.     // If the symbol was resolve to "no_type" then constructor will be NULL
  4215.     //
  4216.     MethodSymbol *constructor = class_creation -> class_type -> symbol -> MethodCast();
  4217.     if (constructor)
  4218.     {
  4219.         class_body -> default_constructor = (AstConstructorDeclaration *) constructor -> method_or_constructor_declaration;
  4220.  
  4221.         if (inner_type -> IsLocal())
  4222.         {
  4223.             inner_type -> AddLocalConstructorCallEnvironment(GetEnvironment(class_creation));
  4224.             UpdateLocalConstructors(inner_type);
  4225.         }
  4226.     }
  4227.  
  4228.     //
  4229.     // TODO: See comment above regarding the setting of this flag.
  4230.     //
  4231.     inner_type -> ResetACC_FINAL();
  4232.  
  4233.     return inner_type;
  4234. }
  4235.  
  4236.  
  4237. void Semantic::ProcessClassInstanceCreationExpression(Ast *expr)
  4238. {
  4239.     AstClassInstanceCreationExpression *class_creation = (AstClassInstanceCreationExpression *) expr;
  4240.  
  4241.     if (class_creation -> base_opt || class_creation -> class_body_opt)
  4242.     {
  4243.         if (! control.option.one_one)
  4244.         {
  4245.             ReportSemError(SemanticError::ONE_ONE_FEATURE,
  4246.                            class_creation -> LeftToken(),
  4247.                            class_creation -> RightToken());
  4248.         }
  4249.     }
  4250.  
  4251.     //
  4252.     // This operation may throw OutOfMemoryError
  4253.     //
  4254.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  4255.     if (exception_set)
  4256.     {
  4257.         exception_set -> AddElement(control.RuntimeException());
  4258.         exception_set -> AddElement(control.Error());
  4259.     }
  4260.  
  4261.     Ast *actual_type = class_creation -> class_type -> type;
  4262.     TypeSymbol *type;
  4263.     if (class_creation -> base_opt)
  4264.     {
  4265.         ProcessExpression(class_creation -> base_opt);
  4266.  
  4267.         TypeSymbol *enclosing_type = class_creation -> base_opt -> Type();
  4268.         if (enclosing_type == control.no_type)
  4269.         {
  4270.             class_creation -> symbol = control.no_type;
  4271.             return;
  4272.         }
  4273.         else if (enclosing_type == control.null_type || enclosing_type -> Primitive())
  4274.         {
  4275.             ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  4276.                            class_creation -> base_opt -> LeftToken(),
  4277.                            class_creation -> base_opt -> RightToken(),
  4278.                            enclosing_type -> ExternalName());
  4279.             class_creation -> symbol = control.no_type;
  4280.             return;
  4281.         }
  4282.  
  4283.         //
  4284.         // The grammar guarantees that the actual type is a simple name.
  4285.         //
  4286.         type = MustFindNestedType(enclosing_type, actual_type);
  4287.         if (type -> ACC_STATIC())
  4288.         {
  4289.             ReportSemError(SemanticError::STATIC_NOT_INNER_CLASS,
  4290.                            actual_type -> LeftToken(),
  4291.                            actual_type -> RightToken(),
  4292.                            type -> ContainingPackage() -> PackageName(),
  4293.                            type -> ExternalName());
  4294.         }
  4295.     }
  4296.     else
  4297.     {
  4298.         type = MustFindType(actual_type);
  4299.         if (type -> IsInner())
  4300.         {
  4301.             //
  4302.             // Within an explicit constructor invocation, a class that is immediately nested
  4303.             // in the class being created is not accessible.
  4304.             //
  4305.             if (ExplicitConstructorInvocation() && type -> ContainingType() == ThisType())
  4306.             {
  4307.                 ReportSemError(SemanticError::INNER_CONSTRUCTOR_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  4308.                                class_creation -> LeftToken(),
  4309.                                class_creation -> RightToken(),
  4310.                                type -> ContainingPackage() -> PackageName(),
  4311.                                type -> ExternalName(),
  4312.                                ThisType() -> ContainingPackage() -> PackageName(),
  4313.                                ThisType() -> ExternalName());
  4314.                 class_creation -> symbol = control.no_type;
  4315.                 return;
  4316.             }
  4317.  
  4318.             class_creation -> base_opt = CreateAccessToType(class_creation, type -> ContainingType());
  4319.         }
  4320.     }
  4321.  
  4322.     bool no_bad_argument = true;
  4323.     for (int i = 0; i < class_creation -> NumArguments(); i++)
  4324.     {
  4325.         AstExpression *expr = class_creation -> Argument(i);
  4326.         ProcessExpressionOrStringConstant(expr);
  4327.         no_bad_argument = no_bad_argument && (expr -> symbol != control.no_type);
  4328.     }
  4329.  
  4330.     TypeSymbol *anonymous_type = NULL;
  4331.  
  4332.     if (! no_bad_argument)
  4333.     {
  4334.         class_creation -> class_type -> symbol = control.no_type;
  4335.         class_creation -> symbol = type;
  4336.     }
  4337.     else
  4338.     {
  4339.         MethodSymbol *method = FindConstructor((type -> ACC_INTERFACE() ? control.Object() : type),
  4340.                                                class_creation,
  4341.                                                actual_type -> LeftToken(),
  4342.                                                class_creation -> right_parenthesis_token);
  4343.  
  4344.         if (! method)
  4345.         {
  4346.             class_creation -> class_type -> symbol = control.no_type;
  4347.             class_creation -> symbol = type;
  4348.         }
  4349.         else
  4350.         {
  4351.             assert(method -> IsTyped());
  4352.  
  4353.             if (class_creation -> base_opt &&
  4354.                 (class_creation -> base_opt -> symbol != control.no_type) &&
  4355.                 (class_creation -> base_opt -> Type() != method -> containing_type -> ContainingType()))
  4356.             {
  4357.                 assert(method -> containing_type);
  4358.                 assert(method -> containing_type -> ContainingType());
  4359.                 assert(class_creation -> base_opt -> Type());
  4360.                 assert(CanMethodInvocationConvert(method -> containing_type -> ContainingType(),
  4361.                                                   class_creation -> base_opt -> Type()));
  4362.  
  4363.                 class_creation -> base_opt = ConvertToType(class_creation -> base_opt, method -> containing_type -> ContainingType());
  4364.             }
  4365.  
  4366.             for (int i = 0; i < class_creation -> NumArguments(); i++)
  4367.             {
  4368.                 AstExpression *expr = class_creation -> Argument(i);
  4369.                 if (expr -> Type() != method -> FormalParameter(i) -> Type())
  4370.                     class_creation -> Argument(i) = ConvertToType(expr, method -> FormalParameter(i) -> Type());
  4371.             }
  4372.  
  4373.             if (class_creation -> class_body_opt)
  4374.                 anonymous_type = GetAnonymousType(class_creation, type);
  4375.             else
  4376.             {
  4377.                 if (type -> ACC_INTERFACE())
  4378.                 {
  4379.                     ReportSemError(SemanticError::NOT_A_CLASS,
  4380.                                    actual_type -> LeftToken(),
  4381.                                    actual_type -> RightToken(),
  4382.                                    type -> ContainingPackage() -> PackageName(),
  4383.                                    type -> ExternalName());
  4384.                     class_creation -> symbol = control.no_type;
  4385.                     return;
  4386.                 }
  4387.                 else if (type -> ACC_ABSTRACT())
  4388.                 {
  4389.                     ReportSemError(SemanticError::ABSTRACT_TYPE_CREATION,
  4390.                                    actual_type -> LeftToken(),
  4391.                                    actual_type -> RightToken(),
  4392.                                    type -> ExternalName());
  4393.                 }
  4394.  
  4395.                 class_creation -> class_type -> symbol = method;
  4396.  
  4397.                 if (exception_set)
  4398.                 {
  4399.                     for (int i = method -> NumThrows() - 1; i >= 0; i--)
  4400.                         exception_set -> AddElement(method -> Throws(i));
  4401.                 }
  4402.  
  4403.                 if (! (ThisType() -> Anonymous() && ThisMethod() && ThisMethod() -> Identity() == control.block_init_name_symbol))
  4404.                 {
  4405.                     for (int k = method -> NumThrows() - 1; k >= 0; k--)
  4406.                     {
  4407.                         TypeSymbol *exception = method -> Throws(k);
  4408.                         if (! CatchableException(exception))
  4409.                         {
  4410.                             ReportSemError(SemanticError::UNCATCHABLE_CONSTRUCTOR_THROWN_CHECKED_EXCEPTION,
  4411.                                            actual_type -> LeftToken(),
  4412.                                            actual_type -> RightToken(),
  4413.                                            type -> ExternalName(),
  4414.                                            exception -> ContainingPackage() -> PackageName(),
  4415.                                            exception -> ExternalName());
  4416.                         }
  4417.                     }
  4418.                 }
  4419.             }
  4420.  
  4421.             class_creation -> symbol = (anonymous_type ? anonymous_type : type);
  4422.  
  4423.             if (ThisType() -> outermost_type == type -> outermost_type &&
  4424.                 (method -> ACC_PRIVATE() && ThisType() != type))
  4425.             {
  4426.                 if (! method -> LocalConstructor())
  4427.                 {
  4428.                     method = TypeSymbol::GetReadAccessMethod(method);
  4429.                     class_creation -> class_type -> symbol = method;
  4430.  
  4431.                     //
  4432.                     // Add extra argument for read access constructor;
  4433.                     //
  4434.                     class_creation -> AddNullArgument();
  4435.                 }
  4436.             }
  4437.             else ConstructorAccessCheck(class_creation, method);
  4438.  
  4439.             //
  4440.             // A local type may use enclosed local variables. So, we at least allocate the
  4441.             // space for adding these extra arguments. If the type being created has already been
  4442.             // fully processed, add the extra arguments here.
  4443.             //
  4444.             if ((! anonymous_type) && type -> IsLocal())
  4445.             {
  4446.                 if (type -> LocalClassProcessingCompleted() && method -> LocalConstructor())
  4447.                 {
  4448.                     assert(! method -> IsGeneratedLocalConstructor());
  4449.  
  4450.                     class_creation -> class_type -> symbol = method -> LocalConstructor();
  4451.  
  4452.                     assert(method -> LocalConstructor() -> signature);
  4453.  
  4454.                     for (int i = (type -> ACC_STATIC() ? 0 : 1); i < type -> NumConstructorParameters(); i++)
  4455.                     {
  4456.                         VariableSymbol *local = type -> ConstructorParameter(i) -> accessed_local;
  4457.  
  4458.                         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(class_creation -> new_token);
  4459.                         //
  4460.                         // Are we currently within the body of the method that contains
  4461.                         // the local type in question?
  4462.                         //
  4463.                         simple_name -> symbol = (type -> owner == ThisMethod()
  4464.                                                                 ? local
  4465.                                                                 : ThisType() -> FindOrInsertLocalShadow(local));
  4466.                         class_creation -> AddLocalArgument(simple_name);
  4467.                     }
  4468.                 }
  4469.                 else // are we within body of type in question? Save processing for later. See ProcessClassDeclaration in body.cpp
  4470.                     type -> AddLocalConstructorCallEnvironment(GetEnvironment(class_creation));
  4471.             }
  4472.         }
  4473.     }
  4474.  
  4475.     return;
  4476. }
  4477.  
  4478.  
  4479. void Semantic::ProcessArrayCreationExpression(Ast *expr)
  4480. {
  4481.     AstArrayCreationExpression *array_creation = (AstArrayCreationExpression *) expr;
  4482.  
  4483.     //
  4484.     // This operation may throw OutOfMemoryError or NegativeArraySizeException
  4485.     //
  4486.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  4487.     if (exception_set)
  4488.     {
  4489.         exception_set -> AddElement(control.RuntimeException());
  4490.         exception_set -> AddElement(control.Error());
  4491.     }
  4492.  
  4493.     AstArrayType *array_type;
  4494.  
  4495.     TypeSymbol *type;
  4496.  
  4497.     if (array_type = array_creation -> array_type -> ArrayTypeCast())
  4498.     {
  4499.         if (! control.option.one_one)
  4500.         {
  4501.             ReportSemError(SemanticError::ONE_ONE_FEATURE,
  4502.                            array_creation -> LeftToken(),
  4503.                            array_creation -> RightToken());
  4504.         }
  4505.  
  4506.         AstPrimitiveType *primitive_type = array_type -> type -> PrimitiveTypeCast();
  4507.         type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(array_type -> type));
  4508.     }
  4509.     else
  4510.     {
  4511.         AstPrimitiveType *primitive_type = array_creation -> array_type -> PrimitiveTypeCast();
  4512.         type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(array_creation -> array_type));
  4513.     }
  4514.  
  4515.     int num_dimensions = (array_type ? array_type -> NumBrackets()
  4516.                                      : array_creation -> NumDimExprs() + array_creation -> NumBrackets());
  4517.  
  4518.     if (num_dimensions > 0)
  4519.         type = type -> GetArrayType((Semantic *) this, num_dimensions);
  4520.     array_creation -> symbol = type;
  4521.  
  4522.     for (int i = 0; i < array_creation -> NumDimExprs(); i++)
  4523.     {
  4524.         AstDimExpr *dim_expr = array_creation -> DimExpr(i);
  4525.         ProcessExpression(dim_expr -> expression);
  4526.         AstExpression *expr = PromoteUnaryNumericExpression(dim_expr -> expression);
  4527.         if (expr -> Type() != control.int_type && expr -> symbol != control.no_type)
  4528.         {
  4529.             ReportSemError(SemanticError::TYPE_NOT_INTEGER,
  4530.                            dim_expr -> expression -> LeftToken(),
  4531.                            dim_expr -> expression -> RightToken(),
  4532.                            dim_expr -> expression -> Type() -> Name());
  4533.         }
  4534.         dim_expr -> expression = expr;
  4535.     }
  4536.  
  4537.     if (array_creation -> array_initializer_opt)
  4538.         ProcessArrayInitializer((AstArrayInitializer *) array_creation -> array_initializer_opt, type);
  4539.  
  4540.     return;
  4541. }
  4542.  
  4543.  
  4544. void Semantic::ProcessPostUnaryExpression(Ast *expr)
  4545. {
  4546.     AstPostUnaryExpression *postfix_expression = (AstPostUnaryExpression *) expr;
  4547.  
  4548.     ProcessExpression(postfix_expression -> expression);
  4549.     postfix_expression -> symbol = postfix_expression -> expression -> symbol;
  4550.  
  4551.     if (postfix_expression -> symbol != control.no_type)
  4552.     {
  4553.         if (! postfix_expression -> expression -> IsLeftHandSide())
  4554.         {
  4555.             ReportSemError(SemanticError::NOT_A_NUMERIC_VARIABLE,
  4556.                            postfix_expression -> expression -> LeftToken(),
  4557.                            postfix_expression -> expression -> RightToken(),
  4558.                            postfix_expression -> expression -> Type() -> Name());
  4559.             postfix_expression -> symbol = control.no_type;
  4560.         }
  4561.         else if (! control.IsNumeric(postfix_expression -> Type()))
  4562.         {
  4563.             ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  4564.                            postfix_expression -> expression -> LeftToken(),
  4565.                            postfix_expression -> expression -> RightToken(),
  4566.                            postfix_expression -> Type() -> Name());
  4567.             postfix_expression -> symbol = control.no_type;
  4568.         }
  4569.         else if (! postfix_expression -> expression -> ArrayAccessCast()) // some kind of name
  4570.         {
  4571.             MethodSymbol *read_method = NULL;
  4572.             AstSimpleName *simple_name = postfix_expression -> expression -> SimpleNameCast();
  4573.             if (simple_name)
  4574.             {
  4575.                 if (simple_name -> resolution_opt)
  4576.                    read_method = simple_name -> resolution_opt -> symbol -> MethodCast();
  4577.             }
  4578.             else
  4579.             {
  4580.                 AstFieldAccess *field_access = (AstFieldAccess *) postfix_expression -> expression;
  4581.                 if (field_access -> resolution_opt)
  4582.                     read_method = field_access -> resolution_opt -> symbol -> MethodCast();
  4583.             }
  4584.  
  4585.             VariableSymbol *variable_symbol;
  4586.             if (read_method)
  4587.             {
  4588.                 variable_symbol = (VariableSymbol *) read_method -> accessed_member;
  4589.                 postfix_expression -> write_method = TypeSymbol::GetWriteAccessMethod(variable_symbol);
  4590.             }
  4591.             else variable_symbol = postfix_expression -> expression -> symbol -> VariableCast();
  4592.         }
  4593.     }
  4594.  
  4595.     return;
  4596. }
  4597.  
  4598.  
  4599. void Semantic::ProcessPLUS(AstPreUnaryExpression *expr)
  4600. {
  4601.     ProcessExpression(expr -> expression);
  4602.     expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
  4603.     expr -> value = expr -> expression -> value;
  4604.     expr -> symbol = expr -> expression -> symbol;
  4605. }
  4606.  
  4607.  
  4608. void Semantic::ProcessMINUS(AstPreUnaryExpression *expr)
  4609. {
  4610.     AstIntegerLiteral *int_literal;
  4611.     AstLongLiteral *long_literal;
  4612.  
  4613.     if (int_literal = expr -> expression -> IntegerLiteralCast())
  4614.     {
  4615.         LiteralSymbol *literal = lex_stream -> LiteralSymbol(int_literal -> integer_literal_token);
  4616.  
  4617.         expr -> value = control.int_pool.FindOrInsertNegativeInt(literal);
  4618.         if (expr -> value == &control.bad_value)
  4619.         {
  4620.             ReportSemError(SemanticError::INVALID_INT_VALUE,
  4621.                            expr -> LeftToken(),
  4622.                            expr -> RightToken());
  4623.             expr -> symbol = control.no_type;
  4624.         }
  4625.         else expr -> symbol = control.int_type;
  4626.     }
  4627.     else if (long_literal = expr -> expression -> LongLiteralCast())
  4628.     {
  4629.         LiteralSymbol *literal = lex_stream -> LiteralSymbol(long_literal -> long_literal_token);
  4630.  
  4631.         expr -> value = control.long_pool.FindOrInsertNegativeLong(literal);
  4632.         if (expr -> value == &control.bad_value)
  4633.         {
  4634.             ReportSemError(SemanticError::INVALID_LONG_VALUE,
  4635.                            expr -> LeftToken(),
  4636.                            expr -> RightToken());
  4637.             expr -> symbol = control.no_type;
  4638.         }
  4639.         else expr -> symbol = control.long_type;
  4640.     }
  4641.     else
  4642.     {
  4643.         ProcessExpression(expr -> expression);
  4644.  
  4645.         expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
  4646.         expr -> symbol = expr -> expression -> symbol;
  4647.         if (expr -> expression -> IsConstant())
  4648.         {
  4649.             TypeSymbol *type = expr -> Type();
  4650.  
  4651.             if (type == control.double_type)
  4652.             {
  4653.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> expression -> value;
  4654.                 expr -> value = control.double_pool.FindOrInsert(-literal -> value);
  4655.             }
  4656.             else if (type == control.float_type)
  4657.             {
  4658.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> expression -> value;
  4659.                 expr -> value = control.float_pool.FindOrInsert(-literal -> value);
  4660.             }
  4661.             else if (type == control.long_type)
  4662.             {
  4663.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> expression -> value;
  4664.                 expr -> value = control.long_pool.FindOrInsert(-literal -> value);
  4665.             }
  4666.             else
  4667.             {
  4668.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> expression -> value;
  4669.                 expr -> value = control.int_pool.FindOrInsert(-literal -> value);
  4670.             }
  4671.         }
  4672.     }
  4673.  
  4674.     return;
  4675. }
  4676.  
  4677.  
  4678. void Semantic::ProcessTWIDDLE(AstPreUnaryExpression *expr)
  4679. {
  4680.     ProcessExpression(expr -> expression);
  4681.  
  4682.     if (expr -> expression -> symbol != control.no_type && (! control.IsIntegral(expr -> expression -> Type())))
  4683.     {
  4684.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  4685.                        expr -> expression -> LeftToken(),
  4686.                        expr -> expression -> RightToken(),
  4687.                        expr -> expression -> Type() -> Name());
  4688.         expr -> symbol = control.no_type;
  4689.     }
  4690.     else
  4691.     {
  4692.         expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
  4693.  
  4694.         if (expr -> expression -> IsConstant())
  4695.         {
  4696.             if (expr -> expression -> Type() == control.long_type)
  4697.             {
  4698.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> expression -> value;
  4699.                 expr -> value = control.long_pool.FindOrInsert(~literal -> value);
  4700.             }
  4701.             else // assert(expr -> expression -> Type() == control.int_type)
  4702.             {
  4703.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> expression -> value;
  4704.                 expr -> value = control.int_pool.FindOrInsert(~literal -> value);
  4705.             }
  4706.         }
  4707.         expr -> symbol = expr -> expression -> symbol;
  4708.     }
  4709.  
  4710.     return;
  4711. }
  4712.  
  4713.  
  4714. void Semantic::ProcessNOT(AstPreUnaryExpression *expr)
  4715. {
  4716.     ProcessExpression(expr -> expression);
  4717.  
  4718.     if (expr -> expression -> symbol != control.no_type && expr -> expression -> Type() != control.boolean_type)
  4719.     {
  4720.         ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  4721.                        expr -> expression -> LeftToken(),
  4722.                        expr -> expression -> RightToken(),
  4723.                        expr -> expression -> Type() -> Name());
  4724.         expr -> symbol = control.no_type;
  4725.     }
  4726.     else
  4727.     {
  4728.         if (expr -> expression -> IsConstant())
  4729.         {
  4730.             IntLiteralValue *literal = (IntLiteralValue *) expr -> expression -> value;
  4731.             expr -> value = control.int_pool.FindOrInsert(literal -> value ? 0 : 1);
  4732.         }
  4733.         expr -> symbol = control.boolean_type;
  4734.     }
  4735.  
  4736.     return;
  4737. }
  4738.  
  4739.  
  4740. void Semantic::ProcessPLUSPLUSOrMINUSMINUS(AstPreUnaryExpression *expr)
  4741. {
  4742.     ProcessExpression(expr -> expression);
  4743.  
  4744.     if (expr -> expression -> symbol != control.no_type)
  4745.     {
  4746.         if (! expr -> expression -> IsLeftHandSide())
  4747.         {
  4748.             ReportSemError(SemanticError::NOT_A_NUMERIC_VARIABLE,
  4749.                            expr -> expression -> LeftToken(),
  4750.                            expr -> expression -> RightToken(),
  4751.                            expr -> expression -> Type() -> Name());
  4752.             expr -> symbol = control.no_type;
  4753.         }
  4754.         else if (! control.IsNumeric(expr -> expression -> Type()))
  4755.         {
  4756.             ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  4757.                            expr -> expression -> LeftToken(),
  4758.                            expr -> expression -> RightToken(),
  4759.                            expr -> expression -> Type() -> Name());
  4760.             expr -> symbol = control.no_type;
  4761.         }
  4762.         else if (! expr -> expression -> ArrayAccessCast()) // some kind of name
  4763.         {
  4764.             MethodSymbol *read_method = NULL;
  4765.             AstSimpleName *simple_name = expr -> expression -> SimpleNameCast();
  4766.             if (simple_name)
  4767.             {
  4768.                 if (simple_name -> resolution_opt)
  4769.                    read_method = simple_name -> resolution_opt -> symbol -> MethodCast();
  4770.             }
  4771.             else
  4772.             {
  4773.                 AstFieldAccess *field_access = (AstFieldAccess *) expr -> expression;
  4774.                 if (field_access -> resolution_opt)
  4775.                     read_method = field_access -> resolution_opt -> symbol -> MethodCast();
  4776.             }
  4777.  
  4778.             VariableSymbol *variable_symbol;
  4779.             if (read_method)
  4780.             {
  4781.                 variable_symbol = (VariableSymbol *) read_method -> accessed_member;
  4782.                 expr -> write_method = TypeSymbol::GetWriteAccessMethod(variable_symbol);
  4783.             }
  4784.             else variable_symbol = expr -> expression -> symbol -> VariableCast();
  4785.         }
  4786.     }
  4787.     expr -> symbol = expr -> expression -> symbol;
  4788.  
  4789.     return;
  4790. }
  4791.  
  4792.  
  4793. void Semantic::ProcessPreUnaryExpression(Ast *expr)
  4794. {
  4795.     AstPreUnaryExpression *prefix_expression = (AstPreUnaryExpression *) expr;
  4796.     (this ->* ProcessPreUnaryExpr[prefix_expression -> pre_unary_tag])(prefix_expression);
  4797.  
  4798.     return;
  4799. }
  4800.  
  4801.  
  4802. inline bool Semantic::CanWideningPrimitiveConvert(TypeSymbol *target_type, TypeSymbol *source_type)
  4803. {
  4804.     if (target_type == control.double_type)
  4805.          return (source_type == control.float_type || source_type == control.long_type  || source_type == control.int_type ||
  4806.                  source_type == control.char_type  || source_type == control.short_type || source_type == control.byte_type);
  4807.     else if (target_type == control.float_type)
  4808.          return (source_type == control.long_type  || source_type == control.int_type   ||
  4809.                  source_type == control.char_type  || source_type == control.short_type || source_type == control.byte_type);
  4810.     else if (target_type == control.long_type)
  4811.          return (source_type == control.int_type   || source_type == control.char_type  ||
  4812.                  source_type == control.short_type || source_type == control.byte_type);
  4813.     else if (target_type == control.int_type)
  4814.          return (source_type == control.char_type  || source_type == control.short_type || source_type == control.byte_type);
  4815.     else if (target_type == control.short_type)
  4816.          return source_type == control.byte_type;
  4817.  
  4818.     return false;
  4819. }
  4820.  
  4821.  
  4822. inline bool Semantic::CanNarrowingPrimitiveConvert(TypeSymbol *target_type, TypeSymbol *source_type)
  4823. {
  4824.     if (target_type == control.byte_type)
  4825.          return (source_type == control.double_type || source_type == control.float_type || source_type == control.long_type ||
  4826.                  source_type == control.int_type    || source_type == control.char_type  || source_type == control.short_type);
  4827.     else if (target_type == control.char_type)
  4828.          return (source_type == control.double_type || source_type == control.float_type || source_type == control.long_type ||
  4829.                  source_type == control.int_type    || source_type == control.short_type || source_type == control.byte_type);
  4830.     else if (target_type == control.short_type)
  4831.          return (source_type == control.double_type || source_type == control.float_type ||
  4832.                  source_type == control.long_type   || source_type == control.int_type   || source_type == control.char_type);
  4833.     else if (target_type == control.int_type)
  4834.          return (source_type == control.double_type || source_type == control.float_type || source_type == control.long_type);
  4835.     else if (target_type == control.long_type)
  4836.          return (source_type == control.double_type || source_type == control.float_type);
  4837.     else if (target_type == control.float_type)
  4838.          return source_type == control.double_type;
  4839.  
  4840.     return false;
  4841. }
  4842.  
  4843.  
  4844. bool Semantic::CanMethodInvocationConvert(TypeSymbol *target_type, TypeSymbol *source_type)
  4845. {
  4846.     if (target_type == control.no_type || source_type == control.no_type)
  4847.         return false;
  4848.  
  4849.     if (source_type -> Primitive())
  4850.     {
  4851.         if (! target_type -> Primitive())
  4852.             return false;
  4853.  
  4854.         return (target_type == source_type || CanWideningPrimitiveConvert(target_type, source_type));
  4855.     }
  4856.     else
  4857.     {
  4858.         if (target_type -> Primitive())
  4859.             return false;
  4860.  
  4861.         if (source_type -> IsArray())
  4862.         {
  4863.             if (target_type -> IsArray())
  4864.             {
  4865.                 TypeSymbol *source_subtype = source_type -> ArraySubtype();
  4866.                 TypeSymbol *target_subtype = target_type -> ArraySubtype();
  4867.                 return (source_subtype -> Primitive() && target_subtype -> Primitive()
  4868.                                                        ? (source_subtype == target_subtype)
  4869.                                                        : CanMethodInvocationConvert(target_subtype, source_subtype));
  4870.             }
  4871.             return (target_type == control.Object() ||
  4872.                     target_type == control.Cloneable() ||
  4873.                     //
  4874.                     // TODO: This is an undocumented feature, but this fix appears to make sense.
  4875.                     //
  4876.                     (control.option.one_one && target_type == control.Serializable() && source_type -> Implements(target_type)));
  4877.         }
  4878.         else if (source_type -> ACC_INTERFACE())
  4879.         {
  4880.             if (target_type -> ACC_INTERFACE())
  4881.                  return source_type -> IsSubinterface(target_type);
  4882.             else if (target_type != control.Object()) // target is a class type
  4883.                  return false;
  4884.         }
  4885.         else if (source_type != control.null_type) // source_type is a class
  4886.         {
  4887.             if (target_type -> IsArray())
  4888.                  return false;
  4889.             else if (target_type -> ACC_INTERFACE())
  4890.                  return source_type -> Implements(target_type);
  4891.             else if (! source_type -> IsSubclass(target_type))
  4892.                  return false;
  4893.         }
  4894.     }
  4895.  
  4896.     return true;
  4897. }
  4898.  
  4899.  
  4900. bool Semantic::CanAssignmentConvertReference(TypeSymbol *target_type, TypeSymbol *source_type)
  4901. {
  4902.     return (target_type == control.no_type ||
  4903.             source_type == control.no_type ||
  4904.             CanMethodInvocationConvert(target_type, source_type)
  4905.            );
  4906. }
  4907.  
  4908.  
  4909. bool Semantic::CanAssignmentConvert(TypeSymbol *target_type, AstExpression *expr)
  4910. {
  4911.     return (target_type == control.no_type ||
  4912.             expr -> symbol == control.no_type ||
  4913.             CanMethodInvocationConvert(target_type, expr -> Type()) ||
  4914.             IsIntValueRepresentableInType(expr, target_type)
  4915.            );
  4916. }
  4917.  
  4918.  
  4919. bool Semantic::CanCastConvert(TypeSymbol *target_type, TypeSymbol *source_type, LexStream::TokenIndex tok)
  4920. {
  4921.     if (source_type == target_type || source_type == control.no_type || target_type == control.no_type)
  4922.         return true;
  4923.  
  4924.     if (source_type -> Primitive())
  4925.     {
  4926.         if (! target_type -> Primitive())
  4927.             return false;
  4928.  
  4929.         return (CanWideningPrimitiveConvert(target_type, source_type) || CanNarrowingPrimitiveConvert(target_type, source_type));
  4930.     }
  4931.     else
  4932.     {
  4933.         if (target_type -> Primitive())
  4934.             return false;
  4935.  
  4936.         if (source_type -> IsArray())
  4937.         {
  4938.             if (target_type -> IsArray())
  4939.             {
  4940.                 TypeSymbol *source_subtype = source_type -> ArraySubtype();
  4941.                 TypeSymbol *target_subtype = target_type -> ArraySubtype();
  4942.                 return (source_subtype -> Primitive() && target_subtype -> Primitive()
  4943.                                                        ? (source_subtype == target_subtype)
  4944.                                                        : CanCastConvert(target_subtype, source_subtype, tok));
  4945.             }
  4946.             return (target_type == control.Object() ||
  4947.                     target_type == control.Cloneable() ||
  4948.                     //
  4949.                     // TODO: This is an undocumented feature, but this fix appears to make sense.
  4950.                     //
  4951.                     (control.option.one_one && target_type == control.Serializable() && source_type -> Implements(target_type)));
  4952.         }
  4953.         else if (source_type -> ACC_INTERFACE())
  4954.         {
  4955.             if (target_type -> ACC_INTERFACE())
  4956.             {
  4957.                 if (! source_type -> expanded_method_table)
  4958.                     ComputeMethodsClosure(source_type, tok);
  4959.                 if (! target_type -> expanded_method_table)
  4960.                     ComputeMethodsClosure(target_type, tok);
  4961.  
  4962.                 //
  4963.                 // Iterate over all methods in the source symbol table of the source_type interface;
  4964.                 // For each such method, if the target_type contains a method with the same signature,
  4965.                 // then make sure that the two methods have the same return type.
  4966.                 //
  4967.                 ExpandedMethodTable *source_method_table = source_type -> expanded_method_table;
  4968.                 int i;
  4969.                 for (i = 0; i < source_method_table -> symbol_pool.Length(); i++)
  4970.                 {
  4971.                     MethodSymbol *method1 = source_method_table -> symbol_pool[i] -> method_symbol;
  4972.                     MethodShadowSymbol *method_shadow2 = target_type -> expanded_method_table
  4973.                                                                      -> FindOverloadMethodShadow(method1, (Semantic *) this, tok);
  4974.                     if (method_shadow2)
  4975.                     {
  4976.                         if (! method1 -> IsTyped())
  4977.                             method1 -> ProcessMethodSignature((Semantic *) this, tok);
  4978.  
  4979.                         MethodSymbol *method2 = method_shadow2 -> method_symbol;
  4980.                         if (! method2 -> IsTyped())
  4981.                             method2 -> ProcessMethodSignature((Semantic *) this, tok);
  4982.                         if (method1 -> Type() != method2 -> Type())
  4983.                             break;
  4984.                     }
  4985.                 }
  4986.  
  4987.                 return (i == source_method_table -> symbol_pool.Length()); // all the methods passed the test
  4988.             }
  4989.             else if (target_type -> ACC_FINAL() && (! target_type -> Implements(source_type)))
  4990.                  return false;
  4991.         }
  4992.         else if (source_type != control.null_type) // source_type is a class
  4993.         {
  4994.             if (target_type -> IsArray())
  4995.             {
  4996.                 if (source_type != control.Object())
  4997.                     return false;
  4998.             }
  4999.             else if (target_type -> ACC_INTERFACE())
  5000.             {
  5001.                 if (source_type -> ACC_FINAL() && (! source_type -> Implements(target_type)))
  5002.                     return false;
  5003.             }
  5004.             else if ((! source_type -> IsSubclass(target_type)) && (! target_type -> IsSubclass(source_type)))
  5005.                  return false;
  5006.         }
  5007.     }
  5008.  
  5009.     return true;
  5010. }
  5011.  
  5012.  
  5013. LiteralValue *Semantic::CastPrimitiveValue(TypeSymbol *target_type, AstExpression *expr)
  5014. {
  5015.     LiteralValue *literal_value = NULL;
  5016.     TypeSymbol *source_type = expr -> Type();
  5017.  
  5018.     if (target_type == source_type)
  5019.         literal_value = expr -> value;
  5020.     else if (source_type != control.no_type)
  5021.     {
  5022.         char output_string[25];
  5023.         int len;
  5024.  
  5025.         if (target_type == control.String())
  5026.         {
  5027.             if (source_type == control.double_type)
  5028.             {
  5029.                 //
  5030.                 // TODO: Check correctness !!!
  5031.                 //
  5032.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5033.                 // sprintf(output_string, "%E", literal -> value);
  5034.                 literal -> value.String(output_string);
  5035.                 len = strlen(output_string);
  5036.                 literal_value = control.Utf8_pool.FindOrInsert(output_string, len);
  5037.             }
  5038.             else if (source_type == control.float_type)
  5039.             {
  5040.                 //
  5041.                 // TODO: Check correctness !!!
  5042.                 //
  5043.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5044.                 // sprintf(output_string, "%E", literal -> value);
  5045.                 literal -> value.String(output_string);
  5046.                 len = strlen(output_string);
  5047.                 //
  5048.                 // javac does not add the L suffix
  5049.                 //
  5050.                 // output_string[len++] = U_F;
  5051.                 //
  5052.                 output_string[len] = U_NULL;
  5053.                 literal_value = control.Utf8_pool.FindOrInsert(output_string, len);
  5054.             }
  5055.             else if (source_type == control.long_type)
  5056.             {
  5057.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5058.                 literal -> value.DecString(output_string);
  5059.                 len = strlen(output_string);
  5060.                 //
  5061.                 // javac does not add the L suffix
  5062.                 //
  5063.                 // output_string[len++] = U_L;
  5064.                 //
  5065.                 output_string[len] = U_NULL;
  5066.                 literal_value = control.Utf8_pool.FindOrInsert(output_string, len);
  5067.             }
  5068.             else if (source_type == control.char_type)
  5069.             {
  5070.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5071.                 literal_value = control.Utf8_pool.FindOrInsert(literal -> value);
  5072.             }
  5073.             else if (source_type == control.boolean_type)
  5074.             {
  5075.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5076.                 if (literal -> value == 0)
  5077.                 {
  5078.                     output_string[0] = U_f;
  5079.                     output_string[1] = U_a;
  5080.                     output_string[2] = U_l;
  5081.                     output_string[3] = U_s;
  5082.                     output_string[4] = U_e;
  5083.                     len = 5;
  5084.                 }
  5085.                 else
  5086.                 {
  5087.                     output_string[0] = U_t;
  5088.                     output_string[1] = U_r;
  5089.                     output_string[2] = U_u;
  5090.                     output_string[3] = U_e;
  5091.                     len = 4;
  5092.                 }
  5093.                 literal_value = control.Utf8_pool.FindOrInsert(output_string, len);
  5094.             }
  5095.             else if (control.IsSimpleIntegerValueType(source_type))
  5096.             {
  5097.                 //
  5098.                 // TODO: Check correctness !!!
  5099.                 //
  5100.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5101.                 sprintf(output_string, "%i", literal -> value);
  5102.                 len = strlen(output_string);
  5103.                 literal_value = control.Utf8_pool.FindOrInsert(output_string, len);
  5104.             }
  5105.             else if (expr -> value == control.NullValue())
  5106.                 literal_value = expr -> value;
  5107.         }
  5108.         else if (target_type == control.double_type)
  5109.         {
  5110.             if (source_type == control.float_type)
  5111.             {
  5112.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5113.                 IEEEdouble value(literal -> value);
  5114.                 literal_value = control.double_pool.FindOrInsert(value);
  5115.             }
  5116.             else if (source_type == control.long_type)
  5117.             {
  5118.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5119.                 IEEEdouble value(literal -> value);
  5120.                 literal_value = control.double_pool.FindOrInsert(value);
  5121.             }
  5122.             else
  5123.             {
  5124.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5125.                 IEEEdouble value(literal -> value);
  5126.                 literal_value = control.double_pool.FindOrInsert(value);
  5127.             }
  5128.         }
  5129.         else if (target_type == control.float_type)
  5130.         {
  5131.             if (source_type == control.double_type)
  5132.             {
  5133.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5134.                 IEEEfloat value(literal -> value);
  5135.                 literal_value = control.float_pool.FindOrInsert(value);
  5136.             }
  5137.             else if (source_type == control.long_type)
  5138.             {
  5139.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5140.                 IEEEfloat value(literal -> value);
  5141.                 literal_value = control.float_pool.FindOrInsert(value);
  5142.             }
  5143.             else
  5144.             {
  5145.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5146.                 IEEEfloat value(literal -> value);
  5147.                 literal_value = control.float_pool.FindOrInsert(value);
  5148.             }
  5149.         }
  5150.         else if (target_type == control.long_type)
  5151.         {
  5152.             if (source_type == control.double_type)
  5153.             {
  5154.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5155.                 LongInt value(literal -> value);
  5156.                 literal_value = control.long_pool.FindOrInsert(value);
  5157.             }
  5158.             else if (source_type == control.float_type)
  5159.             {
  5160.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5161.                 LongInt value(literal -> value);
  5162.                 literal_value = control.long_pool.FindOrInsert(value);
  5163.             }
  5164.             else
  5165.             {
  5166.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5167.                 literal_value = control.long_pool.FindOrInsert((LongInt) literal -> value);
  5168.             }
  5169.         }
  5170.         else if (target_type == control.int_type)
  5171.         {
  5172.             if (source_type == control.double_type)
  5173.             {
  5174.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5175.                 literal_value = control.int_pool.FindOrInsert((literal -> value).IntValue());
  5176.             }
  5177.             else if (source_type == control.float_type)
  5178.             {
  5179.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5180.                 literal_value = control.int_pool.FindOrInsert(literal -> value.IntValue());
  5181.             }
  5182.             else if (source_type == control.long_type)
  5183.             {
  5184.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5185.                 literal_value = control.int_pool.FindOrInsert((int) (literal -> value).LowWord());
  5186.             }
  5187.             else literal_value = expr -> value;
  5188.         }
  5189.         else if (target_type == control.char_type)
  5190.         {
  5191.             if (source_type == control.double_type)
  5192.             {
  5193.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5194.                 literal_value = control.int_pool.FindOrInsert((int) (u2) (literal -> value.IntValue()));
  5195.             }
  5196.             else if (source_type == control.float_type)
  5197.             {
  5198.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5199.                 literal_value = control.int_pool.FindOrInsert((int) (u2) (literal -> value.IntValue()));
  5200.             }
  5201.             else if (source_type == control.long_type)
  5202.             {
  5203.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5204.                 literal_value = control.int_pool.FindOrInsert((int) (u2) (literal -> value).LowWord());
  5205.             }
  5206.             else
  5207.             {
  5208.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5209.                 literal_value = control.int_pool.FindOrInsert((int) (u2) literal -> value);
  5210.             }
  5211.         }
  5212.         else if (target_type == control.short_type)
  5213.         {
  5214.             if (source_type == control.double_type)
  5215.             {
  5216.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5217.                 literal_value = control.int_pool.FindOrInsert((int) (i2) (literal -> value.IntValue()));
  5218.             }
  5219.             else if (source_type == control.float_type)
  5220.             {
  5221.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5222.                 literal_value = control.int_pool.FindOrInsert((int) (i2) (literal -> value.IntValue()));
  5223.             }
  5224.             else if (source_type == control.long_type)
  5225.             {
  5226.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5227.                 literal_value = control.int_pool.FindOrInsert((int) (i2) (literal -> value).LowWord());
  5228.             }
  5229.             else
  5230.             {
  5231.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5232.                 literal_value = control.int_pool.FindOrInsert((int) (i2) literal -> value);
  5233.             }
  5234.         }
  5235.         else if (target_type == control.byte_type)
  5236.         {
  5237.             if (source_type == control.double_type)
  5238.             {
  5239.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5240.                 literal_value = control.int_pool.FindOrInsert((int) (i1) (literal -> value.IntValue()));
  5241.             }
  5242.             else if (source_type == control.float_type)
  5243.             {
  5244.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5245.                 literal_value = control.int_pool.FindOrInsert((int) (i1) (literal -> value.IntValue()));
  5246.             }
  5247.             else if (source_type == control.long_type)
  5248.             {
  5249.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5250.                 literal_value = control.int_pool.FindOrInsert((int) (i1) (literal -> value).LowWord());
  5251.             }
  5252.             else
  5253.             {
  5254.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5255.                 literal_value = control.int_pool.FindOrInsert((int) (i1) literal -> value);
  5256.             }
  5257.         }
  5258.     }
  5259.  
  5260.     return literal_value;
  5261. }
  5262.  
  5263.  
  5264. //
  5265. // We only need to cast the value of constant primitive expressions.
  5266. //
  5267. inline LiteralValue *Semantic::CastValue(TypeSymbol *target_type, AstExpression *expr)
  5268. {
  5269.     return (LiteralValue *) (expr -> IsConstant() && (target_type -> Primitive() || target_type == control.String())
  5270.                                                    ? CastPrimitiveValue(target_type, expr)
  5271.                                                    : NULL);
  5272. }
  5273.  
  5274.  
  5275. void Semantic::ProcessCastExpression(Ast *expr)
  5276. {
  5277.     AstCastExpression *cast_expression = (AstCastExpression *) expr;
  5278.  
  5279.     //
  5280.     // This operation may throw ClassCastException
  5281.     //
  5282.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  5283.     if (exception_set)
  5284.     {
  5285.         exception_set -> AddElement(control.RuntimeException());
  5286.         exception_set -> AddElement(control.Error());
  5287.     }
  5288.  
  5289.     ProcessExpression(cast_expression -> expression);
  5290.  
  5291.     TypeSymbol *source_type = cast_expression -> expression -> Type();
  5292.  
  5293.     //
  5294.     // Recall that the type is optional only when the compiler inserts
  5295.     // a CAST conversion node into the program.
  5296.     //
  5297.     AstPrimitiveType *primitive_type = cast_expression -> type_opt -> PrimitiveTypeCast();
  5298.     TypeSymbol *target_type;
  5299.     if (primitive_type)
  5300.          target_type = FindPrimitiveType(primitive_type);
  5301.     else if (cast_expression -> type_opt -> IsName())
  5302.          target_type = MustFindType(cast_expression -> type_opt);
  5303.     else
  5304.     {
  5305.         ReportSemError(SemanticError::INVALID_CAST_TYPE,
  5306.                        cast_expression -> type_opt -> LeftToken(),
  5307.                        cast_expression -> type_opt -> RightToken());
  5308.         cast_expression -> symbol = control.no_type;
  5309.  
  5310.         return;
  5311.     }
  5312.  
  5313.     int num_dimensions = cast_expression -> NumBrackets();
  5314.     target_type = (num_dimensions == 0 ? target_type : target_type -> GetArrayType((Semantic *) this, num_dimensions));
  5315.  
  5316.     if (CanAssignmentConvert(target_type, cast_expression -> expression))
  5317.     {
  5318.         cast_expression -> symbol = target_type;
  5319.         cast_expression -> value = CastValue(target_type, cast_expression -> expression);
  5320.     }
  5321.     else if (CanCastConvert(target_type, source_type, cast_expression -> right_parenthesis_token_opt))
  5322.     {
  5323.         cast_expression -> kind = Ast::CHECK_AND_CAST;
  5324.         cast_expression -> symbol = target_type;
  5325.         cast_expression -> value = CastValue(target_type, cast_expression -> expression);
  5326.     }
  5327.     else
  5328.     {
  5329.         ReportSemError(SemanticError::INVALID_CAST_CONVERSION,
  5330.                        cast_expression -> expression -> LeftToken(),
  5331.                        cast_expression -> expression -> RightToken(),
  5332.                        source_type -> Name(),
  5333.                        target_type -> Name());
  5334.         cast_expression -> symbol = control.no_type;
  5335.     }
  5336.  
  5337.     return;
  5338. }
  5339.  
  5340.  
  5341. AstExpression *Semantic::ConvertToType(AstExpression *expr, TypeSymbol *type)
  5342. {
  5343.     if (expr -> Type() == control.null_type)
  5344.         return expr;
  5345.  
  5346.     LexStream::TokenIndex loc = expr -> LeftToken();
  5347.  
  5348.     AstCastExpression *result = compilation_unit -> ast_pool -> GenCastExpression();
  5349.     result -> left_parenthesis_token_opt = loc;
  5350.     result -> type_opt = NULL;
  5351.     result -> right_parenthesis_token_opt = loc;
  5352.     result -> expression = expr;
  5353.  
  5354.     result -> symbol = type;
  5355.     result -> value = CastValue(type, expr);
  5356.  
  5357.     return result;
  5358. }
  5359.  
  5360.  
  5361. AstExpression *Semantic::PromoteUnaryNumericExpression(AstExpression *unary_expression)
  5362. {
  5363.     TypeSymbol *type = unary_expression -> Type();
  5364.  
  5365.     if (type == control.no_type)
  5366.         return unary_expression;
  5367.  
  5368.     if (! control.IsNumeric(type))
  5369.     {
  5370.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5371.                       unary_expression -> LeftToken(),
  5372.                       unary_expression -> RightToken(),
  5373.                       type -> Name());
  5374.         unary_expression -> symbol = control.no_type;
  5375.         return unary_expression;
  5376.     }
  5377.  
  5378.     return ((type == control.byte_type || type == control.short_type || type == control.char_type)
  5379.                                                 ? ConvertToType(unary_expression, control.int_type)
  5380.                                                 : unary_expression);
  5381. }
  5382.  
  5383.  
  5384. void Semantic::BinaryNumericPromotion(AstBinaryExpression *binary_expression)
  5385. {
  5386.     AstExpression *left_expr = binary_expression -> left_expression;
  5387.     AstExpression *right_expr = binary_expression -> right_expression;
  5388.  
  5389.     TypeSymbol *left_type  = left_expr -> Type(),
  5390.                *right_type = right_expr -> Type();
  5391.  
  5392.     if (left_type == control.no_type || right_type == control.no_type)
  5393.     {
  5394.         binary_expression -> symbol = control.no_type;
  5395.         return;
  5396.     }
  5397.  
  5398.     if (! control.IsNumeric(left_type))
  5399.     {
  5400.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5401.                       left_expr -> LeftToken(),
  5402.                       left_expr -> RightToken(),
  5403.                       left_type -> Name());
  5404.         binary_expression -> symbol = control.no_type;
  5405.         return;
  5406.     }
  5407.     else if (! control.IsNumeric(right_type))
  5408.     {
  5409.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5410.                       right_expr -> LeftToken(),
  5411.                       right_expr -> RightToken(),
  5412.                       right_type -> Name());
  5413.         binary_expression -> symbol = control.no_type;
  5414.         return;
  5415.     }
  5416.  
  5417.     if (left_type == control.double_type)
  5418.     {
  5419.         if (right_type != control.double_type)
  5420.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.double_type);
  5421.         binary_expression -> symbol = control.double_type;
  5422.     }
  5423.     else if (right_type == control.double_type)
  5424.     {
  5425.         if (left_type != control.double_type)
  5426.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.double_type);
  5427.         binary_expression -> symbol = control.double_type;
  5428.     }
  5429.     else if (left_type == control.float_type)
  5430.     {
  5431.         if (right_type != control.float_type)
  5432.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.float_type);
  5433.         binary_expression -> symbol = control.float_type;
  5434.     }
  5435.     else if (right_type == control.float_type)
  5436.     {
  5437.         if (left_type != control.float_type)
  5438.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.float_type);
  5439.         binary_expression -> symbol = control.float_type;
  5440.     }
  5441.     else if (left_type == control.long_type)
  5442.     {
  5443.         if (right_type != control.long_type)
  5444.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.long_type);
  5445.         binary_expression -> symbol = control.long_type;
  5446.     }
  5447.     else if (right_type == control.long_type)
  5448.     {
  5449.         if (left_type != control.long_type)
  5450.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.long_type);
  5451.         binary_expression -> symbol = control.long_type;
  5452.     }
  5453.     else
  5454.     {
  5455.         if (left_type != control.int_type)
  5456.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.int_type);
  5457.         if (right_type != control.int_type)
  5458.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.int_type);
  5459.         binary_expression -> symbol = control.int_type;
  5460.     }
  5461.  
  5462.     return;
  5463. }
  5464.  
  5465.  
  5466. void Semantic::BinaryNumericPromotion(AstAssignmentExpression *assignment_expression)
  5467. {
  5468.     AstExpression *left_expr = assignment_expression -> left_hand_side;
  5469.     AstExpression *right_expr = assignment_expression -> expression;
  5470.  
  5471.     TypeSymbol *left_type  = left_expr -> Type(),
  5472.                *right_type = right_expr -> Type();
  5473.  
  5474.     if (left_type == control.no_type || right_type == control.no_type)
  5475.         return;
  5476.  
  5477.     if (! control.IsNumeric(left_type))
  5478.     {
  5479.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5480.                       left_expr -> LeftToken(),
  5481.                       left_expr -> RightToken(),
  5482.                       left_type -> Name());
  5483.         return;
  5484.     }
  5485.     else if (! control.IsNumeric(right_type))
  5486.     {
  5487.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5488.                       right_expr -> LeftToken(),
  5489.                       right_expr -> RightToken(),
  5490.                       right_type -> Name());
  5491.         return;
  5492.     }
  5493.  
  5494.     if (left_type == control.double_type)
  5495.     {
  5496.         if (right_type != control.double_type)
  5497.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.double_type);
  5498.     }
  5499.     else if (right_type == control.double_type)
  5500.     {
  5501.         if (left_type != control.double_type)
  5502.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.double_type);
  5503.     }
  5504.     else if (left_type == control.float_type)
  5505.     {
  5506.         if (right_type != control.float_type)
  5507.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.float_type);
  5508.     }
  5509.     else if (right_type == control.float_type)
  5510.     {
  5511.         if (left_type != control.float_type)
  5512.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.float_type);
  5513.     }
  5514.     else if (left_type == control.long_type)
  5515.     {
  5516.         if (right_type != control.long_type)
  5517.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.long_type);
  5518.     }
  5519.     else if (right_type == control.long_type)
  5520.     {
  5521.         if (left_type != control.long_type)
  5522.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.long_type);
  5523.     }
  5524.     else
  5525.     {
  5526.         if (left_type != control.int_type)
  5527.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.int_type);
  5528.         if (right_type != control.int_type)
  5529.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.int_type);
  5530.     }
  5531.  
  5532.     return;
  5533. }
  5534.  
  5535.  
  5536. void Semantic::BinaryNumericPromotion(AstConditionalExpression *conditional_expression)
  5537. {
  5538.     AstExpression *left_expr = conditional_expression -> true_expression;
  5539.     AstExpression *right_expr = conditional_expression -> false_expression;
  5540.  
  5541.     TypeSymbol *left_type  = left_expr -> Type(),
  5542.                *right_type = right_expr -> Type();
  5543.  
  5544.     if (left_type == control.no_type || right_type == control.no_type)
  5545.     {
  5546.         conditional_expression -> symbol = control.no_type;
  5547.         return;
  5548.     }
  5549.  
  5550.     if (! control.IsNumeric(left_type))
  5551.     {
  5552.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5553.                       left_expr -> LeftToken(),
  5554.                       left_expr -> RightToken(),
  5555.                       left_type -> Name());
  5556.         conditional_expression -> symbol = control.no_type;
  5557.         return;
  5558.     }
  5559.     else if (! control.IsNumeric(right_type))
  5560.     {
  5561.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5562.                       right_expr -> LeftToken(),
  5563.                       right_expr -> RightToken(),
  5564.                       right_type -> Name());
  5565.         conditional_expression -> symbol = control.no_type;
  5566.         return;
  5567.     }
  5568.  
  5569.     if (left_type == control.double_type)
  5570.     {
  5571.         if (right_type != control.double_type)
  5572.             conditional_expression -> false_expression =
  5573.                         ConvertToType(conditional_expression -> false_expression, control.double_type);
  5574.         conditional_expression -> symbol = control.double_type;
  5575.     }
  5576.     else if (right_type == control.double_type)
  5577.     {
  5578.         if (left_type != control.double_type)
  5579.             conditional_expression -> true_expression =
  5580.                         ConvertToType(conditional_expression -> true_expression, control.double_type);
  5581.         conditional_expression -> symbol = control.double_type;
  5582.     }
  5583.     else if (left_type == control.float_type)
  5584.     {
  5585.         if (right_type != control.float_type)
  5586.             conditional_expression -> false_expression =
  5587.                         ConvertToType(conditional_expression -> false_expression, control.float_type);
  5588.         conditional_expression -> symbol = control.float_type;
  5589.     }
  5590.     else if (right_type == control.float_type)
  5591.     {
  5592.         if (left_type != control.float_type)
  5593.             conditional_expression -> true_expression =
  5594.                         ConvertToType(conditional_expression -> true_expression, control.float_type);
  5595.         conditional_expression -> symbol = control.float_type;
  5596.     }
  5597.     else if (left_type == control.long_type)
  5598.     {
  5599.         if (right_type != control.long_type)
  5600.             conditional_expression -> false_expression =
  5601.                         ConvertToType(conditional_expression -> false_expression, control.long_type);
  5602.         conditional_expression -> symbol = control.long_type;
  5603.     }
  5604.     else if (right_type == control.long_type)
  5605.     {
  5606.         if (left_type != control.long_type)
  5607.             conditional_expression -> true_expression = ConvertToType(conditional_expression -> true_expression, control.long_type);
  5608.         conditional_expression -> symbol = control.long_type;
  5609.     }
  5610.     else
  5611.     {
  5612.         if (left_type != control.int_type)
  5613.             conditional_expression -> true_expression = ConvertToType(conditional_expression -> true_expression, control.int_type);
  5614.         if (right_type != control.int_type)
  5615.             conditional_expression -> false_expression =
  5616.                         ConvertToType(conditional_expression -> false_expression, control.int_type);
  5617.         conditional_expression -> symbol = control.int_type;
  5618.     }
  5619.  
  5620.     return;
  5621. }
  5622.  
  5623.  
  5624. void Semantic::ProcessPLUS(AstBinaryExpression *expr)
  5625. {
  5626.     ProcessExpression(expr -> left_expression);
  5627.     ProcessExpression(expr -> right_expression);
  5628.  
  5629.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5630.                *right_type = expr -> right_expression -> Type();
  5631.  
  5632.     if (left_type == control.no_type || right_type == control.no_type)
  5633.         expr -> symbol = control.no_type;
  5634.     else if (left_type == control.String() || right_type == control.String())
  5635.     {
  5636.         //
  5637.         // This operation may throw OutOfMemoryException
  5638.         //
  5639.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  5640.         if (exception_set)
  5641.         {
  5642.             exception_set -> AddElement(control.RuntimeException());
  5643.             exception_set -> AddElement(control.Error());
  5644.         }
  5645.  
  5646.         if (left_type != control.String())
  5647.         {
  5648.             AddStringConversionDependence(left_type, expr -> binary_operator_token);
  5649.             AstExpression *left_expression = expr -> left_expression;
  5650.             if (left_type == control.void_type)
  5651.                 ReportSemError(SemanticError::VOID_TO_STRING,
  5652.                                left_expression -> LeftToken(),
  5653.                                left_expression -> RightToken());
  5654.             expr -> left_expression = ConvertToType(left_expression, control.String());
  5655.             if (left_expression -> IsConstant())
  5656.                 expr -> left_expression -> value = CastPrimitiveValue(control.String(), left_expression);
  5657.         }
  5658.         else if (right_type != control.String())
  5659.         {
  5660.             AddStringConversionDependence(right_type, expr -> binary_operator_token);
  5661.             AstExpression *right_expression = expr -> right_expression;
  5662.             if (right_type == control.void_type)
  5663.                 ReportSemError(SemanticError::VOID_TO_STRING,
  5664.                                right_expression -> LeftToken(),
  5665.                                right_expression -> RightToken());
  5666.             expr -> right_expression = ConvertToType(right_expression, control.String());
  5667.             if (right_expression -> IsConstant())
  5668.                 expr -> right_expression -> value = CastPrimitiveValue(control.String(), right_expression);
  5669.         }
  5670.         AddDependence(ThisType(), control.StringBuffer(), expr -> binary_operator_token);
  5671.  
  5672.         //
  5673.         // If both subexpressions are strings constants, identify the result as
  5674.         // as a string constant, but do not perform the concatenation here. The
  5675.         // reason being that if we have a long expression of the form
  5676.         //
  5677.         //  s1 + s2 + ... + sn
  5678.         //
  5679.         // where each subexpression s(i) is a string constant, we want to perform
  5680.         // one concatenation and enter a single result into the constant pool instead
  5681.         // of n-1 subresults. See CheckStringConstant in lookup.cpp.
  5682.         //
  5683.  
  5684.         expr -> symbol = control.String();
  5685.     }
  5686.     else
  5687.     {
  5688.         BinaryNumericPromotion(expr);
  5689.  
  5690.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5691.         {
  5692.              if (expr -> Type() == control.double_type)
  5693.              {
  5694.                  DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  5695.                  DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  5696.  
  5697.                  expr -> value = control.double_pool.FindOrInsert(left -> value + right -> value);
  5698.              }
  5699.              else if (expr -> Type() == control.float_type)
  5700.              {
  5701.                  FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  5702.                  FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  5703.  
  5704.                  expr -> value = control.float_pool.FindOrInsert(left -> value + right -> value);
  5705.              }
  5706.              else if (expr -> Type() == control.long_type)
  5707.              {
  5708.                  LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5709.                  LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  5710.  
  5711.                  expr -> value = control.long_pool.FindOrInsert(left -> value + right -> value);
  5712.              }
  5713.              else // assert(expr -> Type() == control.int_type)
  5714.              {
  5715.                  IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5716.                  IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5717.  
  5718.                  expr -> value = control.int_pool.FindOrInsert(left -> value + right -> value);
  5719.              }
  5720.         }
  5721.     }
  5722.  
  5723.     return;
  5724. }
  5725.  
  5726.  
  5727. void Semantic::ProcessLEFT_SHIFT(AstBinaryExpression *expr)
  5728. {
  5729.     ProcessExpression(expr -> left_expression);
  5730.     ProcessExpression(expr -> right_expression);
  5731.  
  5732.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5733.                *right_type = expr -> right_expression -> Type();
  5734.  
  5735.     if (left_type == control.no_type || right_type == control.no_type)
  5736.         expr -> symbol = control.no_type;
  5737.     else if (! control.IsIntegral(left_type))
  5738.     {
  5739.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5740.                        expr -> left_expression -> LeftToken(),
  5741.                        expr -> left_expression -> RightToken(),
  5742.                        left_type -> Name());
  5743.         expr -> symbol = control.no_type;
  5744.     }
  5745.     else if (! control.IsIntegral(right_type))
  5746.     {
  5747.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5748.                        expr -> right_expression -> LeftToken(),
  5749.                        expr -> right_expression -> RightToken(),
  5750.                        right_type -> Name());
  5751.         expr -> symbol = control.no_type;
  5752.     }
  5753.     else
  5754.     {
  5755.         expr -> left_expression  = PromoteUnaryNumericExpression(expr -> left_expression);
  5756.         expr -> right_expression = PromoteUnaryNumericExpression(expr -> right_expression);
  5757.         if (expr -> right_expression -> Type() == control.long_type)
  5758.             expr -> right_expression = ConvertToType(expr -> right_expression, control.int_type);
  5759.         expr -> symbol = expr -> left_expression -> symbol;
  5760.  
  5761.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5762.         {
  5763.             if (expr -> Type() == control.long_type)
  5764.             {
  5765.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5766.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5767.  
  5768.                 expr -> value = control.long_pool.FindOrInsert(left -> value << (right -> value & 0x3F));
  5769.             }
  5770.             else // assert(expr -> Type() == control.int_type)
  5771.             {
  5772.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5773.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5774.  
  5775.                 expr -> value = control.int_pool.FindOrInsert(left -> value << (0x1F & right -> value));
  5776.             }
  5777.         }
  5778.     }
  5779.  
  5780.     return;
  5781. }
  5782.  
  5783.  
  5784. void Semantic::ProcessRIGHT_SHIFT(AstBinaryExpression *expr)
  5785. {
  5786.     ProcessExpression(expr -> left_expression);
  5787.     ProcessExpression(expr -> right_expression);
  5788.  
  5789.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5790.                *right_type = expr -> right_expression -> Type();
  5791.  
  5792.     if (left_type == control.no_type || right_type == control.no_type)
  5793.         expr -> symbol = control.no_type;
  5794.     else if (! control.IsIntegral(left_type))
  5795.     {
  5796.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5797.                        expr -> left_expression -> LeftToken(),
  5798.                        expr -> left_expression -> RightToken(),
  5799.                        left_type -> Name());
  5800.         expr -> symbol = control.no_type;
  5801.     }
  5802.     else if (! control.IsIntegral(right_type))
  5803.     {
  5804.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5805.                        expr -> right_expression -> LeftToken(),
  5806.                        expr -> right_expression -> RightToken(),
  5807.                        right_type -> Name());
  5808.         expr -> symbol = control.no_type;
  5809.     }
  5810.     else
  5811.     {
  5812.         expr -> left_expression  = PromoteUnaryNumericExpression(expr -> left_expression);
  5813.         expr -> right_expression = PromoteUnaryNumericExpression(expr -> right_expression);
  5814.         if (expr -> right_expression -> Type() == control.long_type)
  5815.             expr -> right_expression = ConvertToType(expr -> right_expression, control.int_type);
  5816.         expr -> symbol = expr -> left_expression -> symbol;
  5817.  
  5818.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5819.         {
  5820.             if (expr -> Type() == control.long_type)
  5821.             {
  5822.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5823.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5824.  
  5825.                 expr -> value = control.long_pool.FindOrInsert(left -> value >> (right -> value & 0x3F));
  5826.             }
  5827.             else // assert(expr -> Type() == control.int_type)
  5828.             {
  5829.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5830.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5831.  
  5832.                 expr -> value = control.int_pool.FindOrInsert(left -> value >> (0x1F & right -> value));
  5833.             }
  5834.         }
  5835.     }
  5836.  
  5837.     return;
  5838. }
  5839.  
  5840.  
  5841. void Semantic::ProcessUNSIGNED_RIGHT_SHIFT(AstBinaryExpression *expr)
  5842. {
  5843.     ProcessExpression(expr -> left_expression);
  5844.     ProcessExpression(expr -> right_expression);
  5845.  
  5846.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5847.                *right_type = expr -> right_expression -> Type();
  5848.  
  5849.     if (left_type == control.no_type || right_type == control.no_type)
  5850.         expr -> symbol = control.no_type;
  5851.     else if (! control.IsIntegral(left_type))
  5852.     {
  5853.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5854.                        expr -> left_expression -> LeftToken(),
  5855.                        expr -> left_expression -> RightToken(),
  5856.                        left_type -> Name());
  5857.         expr -> symbol = control.no_type;
  5858.     }
  5859.     else if (! control.IsIntegral(right_type))
  5860.     {
  5861.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5862.                        expr -> right_expression -> LeftToken(),
  5863.                        expr -> right_expression -> RightToken(),
  5864.                        right_type -> Name());
  5865.         expr -> symbol = control.no_type;
  5866.     }
  5867.     else
  5868.     {
  5869.         expr -> left_expression  = PromoteUnaryNumericExpression(expr -> left_expression);
  5870.         expr -> right_expression = PromoteUnaryNumericExpression(expr -> right_expression);
  5871.         if (expr -> right_expression -> Type() == control.long_type)
  5872.             expr -> right_expression = ConvertToType(expr -> right_expression, control.int_type);
  5873.         expr -> symbol = expr -> left_expression -> symbol;
  5874.  
  5875.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5876.         {
  5877.             if (expr -> Type() == control.long_type)
  5878.             {
  5879.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5880.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5881.                 int right_value = right -> value & 0x3F;
  5882.  
  5883.                 LongInt value = left -> value >> right_value;
  5884.                 if (left -> value < 0)
  5885.                     value += (LongInt(2) << (63 - right_value));
  5886.                 expr -> value = control.long_pool.FindOrInsert(value);
  5887.             }
  5888.             else // assert(expr -> Type() == control.int_type)
  5889.             {
  5890.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5891.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5892.  
  5893.                 int value = left -> value >> (0x1F & right -> value);
  5894.                 if (left -> value < 0)
  5895.                      value += (2 << (31 - (0x1F & right -> value)));
  5896.                 expr -> value = control.int_pool.FindOrInsert(value);
  5897.             }
  5898.         }
  5899.     }
  5900.  
  5901.     return;
  5902. }
  5903.  
  5904.  
  5905. void Semantic::ProcessLESS(AstBinaryExpression *expr)
  5906. {
  5907.     ProcessExpression(expr -> left_expression);
  5908.     ProcessExpression(expr -> right_expression);
  5909.  
  5910.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5911.                *right_type = expr -> right_expression -> Type();
  5912.  
  5913.     if (left_type != control.no_type && right_type != control.no_type)
  5914.     {
  5915.         BinaryNumericPromotion(expr);
  5916.  
  5917.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5918.         {
  5919.             if (expr -> Type() == control.double_type)
  5920.             {
  5921.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  5922.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  5923.  
  5924.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5925.             }
  5926.             else if (expr -> Type() == control.float_type)
  5927.             {
  5928.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  5929.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  5930.  
  5931.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5932.             }
  5933.             else if (expr -> Type() == control.long_type)
  5934.            {
  5935.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5936.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  5937.  
  5938.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5939.             }
  5940.             else // assert(expr -> Type() == control.int_type)
  5941.             {
  5942.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5943.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5944.  
  5945.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5946.             }
  5947.         }
  5948.     }
  5949.  
  5950.     expr -> symbol = control.boolean_type;
  5951.  
  5952.     return;
  5953. }
  5954.  
  5955.  
  5956. void Semantic::ProcessGREATER(AstBinaryExpression *expr)
  5957. {
  5958.     ProcessExpression(expr -> left_expression);
  5959.     ProcessExpression(expr -> right_expression);
  5960.  
  5961.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5962.                *right_type = expr -> right_expression -> Type();
  5963.  
  5964.     if (left_type != control.no_type && right_type != control.no_type)
  5965.     {
  5966.         BinaryNumericPromotion(expr);
  5967.  
  5968.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5969.         {
  5970.             if (expr -> Type() == control.double_type)
  5971.             {
  5972.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  5973.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  5974.  
  5975.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  5976.             }
  5977.             else if (expr -> Type() == control.float_type)
  5978.             {
  5979.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  5980.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  5981.  
  5982.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  5983.             }
  5984.             else if (expr -> Type() == control.long_type)
  5985.             {
  5986.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5987.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  5988.  
  5989.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  5990.             }
  5991.             else // assert(expr -> Type() == control.int_type)
  5992.             {
  5993.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5994.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5995.  
  5996.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  5997.             }
  5998.         }
  5999.     }
  6000.  
  6001.     expr -> symbol = control.boolean_type;
  6002.  
  6003.     return;
  6004. }
  6005.  
  6006.  
  6007. void Semantic::ProcessLESS_EQUAL(AstBinaryExpression *expr)
  6008. {
  6009.     ProcessExpression(expr -> left_expression);
  6010.     ProcessExpression(expr -> right_expression);
  6011.  
  6012.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6013.                *right_type = expr -> right_expression -> Type();
  6014.  
  6015.     if (left_type != control.no_type && right_type != control.no_type)
  6016.     {
  6017.         BinaryNumericPromotion(expr);
  6018.  
  6019.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6020.         {
  6021.             if (expr -> Type() == control.double_type)
  6022.             {
  6023.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6024.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6025.  
  6026.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6027.             }
  6028.             else if (expr -> Type() == control.float_type)
  6029.             {
  6030.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6031.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6032.  
  6033.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6034.             }
  6035.             else if (expr -> Type() == control.long_type)
  6036.             {
  6037.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6038.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6039.  
  6040.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6041.             }
  6042.             else // assert(expr -> Type() == control.int_type)
  6043.             {
  6044.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6045.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6046.  
  6047.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6048.             }
  6049.         }
  6050.     }
  6051.  
  6052.     expr -> symbol = control.boolean_type;
  6053.  
  6054.     return;
  6055. }
  6056.  
  6057.  
  6058. void Semantic::ProcessGREATER_EQUAL(AstBinaryExpression *expr)
  6059. {
  6060.     ProcessExpression(expr -> left_expression);
  6061.     ProcessExpression(expr -> right_expression);
  6062.  
  6063.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6064.                *right_type = expr -> right_expression -> Type();
  6065.  
  6066.     if (left_type != control.no_type && right_type != control.no_type)
  6067.     {
  6068.         BinaryNumericPromotion(expr);
  6069.  
  6070.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6071.         {
  6072.             if (expr -> Type() == control.double_type)
  6073.             {
  6074.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6075.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6076.  
  6077.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6078.             }
  6079.             else if (expr -> Type() == control.float_type)
  6080.             {
  6081.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6082.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6083.  
  6084.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6085.             }
  6086.             else if (expr -> Type() == control.long_type)
  6087.             {
  6088.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6089.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6090.  
  6091.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6092.             }
  6093.             else // assert(expr -> Type() == control.int_type)
  6094.             {
  6095.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6096.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6097.  
  6098.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6099.             }
  6100.         }
  6101.     }
  6102.  
  6103.     expr -> symbol = control.boolean_type;
  6104.  
  6105.     return;
  6106. }
  6107.  
  6108.  
  6109. void Semantic::ProcessAND(AstBinaryExpression *expr)
  6110. {
  6111.     ProcessExpression(expr -> left_expression);
  6112.     ProcessExpression(expr -> right_expression);
  6113.  
  6114.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6115.                *right_type = expr -> right_expression -> Type();
  6116.  
  6117.     if (left_type == control.no_type || right_type == control.no_type)
  6118.         expr -> symbol = control.no_type;
  6119.     else
  6120.     {
  6121.         if (left_type == control.boolean_type && right_type == control.boolean_type)
  6122.              expr -> symbol = control.boolean_type;
  6123.         else
  6124.         {
  6125.             BinaryNumericPromotion(expr);
  6126.  
  6127.             TypeSymbol *expr_type = expr -> Type();
  6128.  
  6129.             if (expr_type != control.no_type)
  6130.             {
  6131.                 if (! control.IsIntegral(expr_type))
  6132.                 {
  6133.                     ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  6134.                                    expr -> LeftToken(),
  6135.                                    expr -> RightToken(),
  6136.                                    expr_type -> Name());
  6137.                     expr -> symbol = control.no_type;
  6138.                 }
  6139.                 else if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6140.                 {
  6141.                     if (expr_type == control.long_type)
  6142.                     {
  6143.                         LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6144.                         LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6145.  
  6146.                         expr -> value = control.long_pool.FindOrInsert(left -> value & right -> value);
  6147.                     }
  6148.                     else // assert(expr_type == control.int_type)
  6149.                     {
  6150.                         IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6151.                         IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6152.  
  6153.                         expr -> value = control.int_pool.FindOrInsert(left -> value & right -> value);
  6154.                     }
  6155.                 }
  6156.             }
  6157.         }
  6158.     }
  6159.  
  6160.     return;
  6161. }
  6162.  
  6163.  
  6164. void Semantic::ProcessXOR(AstBinaryExpression *expr)
  6165. {
  6166.     ProcessExpression(expr -> left_expression);
  6167.     ProcessExpression(expr -> right_expression);
  6168.  
  6169.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6170.                *right_type = expr -> right_expression -> Type();
  6171.  
  6172.     if (left_type == control.no_type || right_type == control.no_type)
  6173.         expr -> symbol = control.no_type;
  6174.     else
  6175.     {
  6176.         if (left_type == control.boolean_type && right_type == control.boolean_type)
  6177.              expr -> symbol = control.boolean_type;
  6178.         else
  6179.         {
  6180.             BinaryNumericPromotion(expr);
  6181.  
  6182.             TypeSymbol *expr_type = expr -> Type();
  6183.  
  6184.             if (expr_type != control.no_type)
  6185.             {
  6186.                 if (! control.IsIntegral(expr_type))
  6187.                 {
  6188.                     ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  6189.                                    expr -> LeftToken(),
  6190.                                    expr -> RightToken(),
  6191.                                    expr_type -> Name());
  6192.                     expr -> symbol = control.no_type;
  6193.                 }
  6194.                 else if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6195.                 {
  6196.                     if (expr_type == control.long_type)
  6197.                     {
  6198.                         LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6199.                         LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6200.  
  6201.                         expr -> value = control.long_pool.FindOrInsert(left -> value ^ right -> value);
  6202.                     }
  6203.                     else // assert(expr_type == control.int_type)
  6204.                     {
  6205.                         IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6206.                         IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6207.  
  6208.                         expr -> value = control.int_pool.FindOrInsert(left -> value ^ right -> value);
  6209.                     }
  6210.                 }
  6211.             }
  6212.         }
  6213.     }
  6214.  
  6215.     return;
  6216. }
  6217.  
  6218.  
  6219. void Semantic::ProcessIOR(AstBinaryExpression *expr)
  6220. {
  6221.     ProcessExpression(expr -> left_expression);
  6222.     ProcessExpression(expr -> right_expression);
  6223.  
  6224.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6225.                *right_type = expr -> right_expression -> Type();
  6226.  
  6227.     if (left_type == control.no_type || right_type == control.no_type)
  6228.         expr -> symbol = control.no_type;
  6229.     else
  6230.     {
  6231.         if (left_type == control.boolean_type && right_type == control.boolean_type)
  6232.              expr -> symbol = control.boolean_type;
  6233.         else
  6234.         {
  6235.             BinaryNumericPromotion(expr);
  6236.  
  6237.             TypeSymbol *expr_type = expr -> Type();
  6238.  
  6239.             if (expr_type != control.no_type)
  6240.             {
  6241.                 if (! control.IsIntegral(expr_type))
  6242.                 {
  6243.                     ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  6244.                                    expr -> LeftToken(),
  6245.                                    expr -> RightToken(),
  6246.                                    expr_type -> Name());
  6247.                     expr -> symbol = control.no_type;
  6248.                 }
  6249.                 else if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6250.                 {
  6251.                     if (expr_type == control.long_type)
  6252.                     {
  6253.                         LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6254.                         LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6255.  
  6256.                         expr -> value = control.long_pool.FindOrInsert(left -> value | right -> value);
  6257.                     }
  6258.                     else // assert(expr_type == control.int_type)
  6259.                     {
  6260.                         IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6261.                         IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6262.  
  6263.                         expr -> value = control.int_pool.FindOrInsert(left -> value | right -> value);
  6264.                     }
  6265.                 }
  6266.             }
  6267.         }
  6268.     }
  6269.  
  6270.     return;
  6271. }
  6272.  
  6273.  
  6274. void Semantic::ProcessAND_AND(AstBinaryExpression *expr)
  6275. {
  6276.     ProcessExpression(expr -> left_expression);
  6277.     ProcessExpression(expr -> right_expression);
  6278.  
  6279.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6280.                *right_type = expr -> right_expression -> Type();
  6281.  
  6282.     if (left_type != control.no_type && right_type != control.no_type)
  6283.     {
  6284.         if (left_type != control.boolean_type)
  6285.         {
  6286.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6287.                            expr -> left_expression -> LeftToken(),
  6288.                            expr -> left_expression -> RightToken(),
  6289.                            left_type -> Name());
  6290.         }
  6291.         else if (right_type != control.boolean_type)
  6292.         {
  6293.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6294.                            expr -> right_expression -> LeftToken(),
  6295.                            expr -> right_expression -> RightToken(),
  6296.                            right_type -> Name());
  6297.         }
  6298.         else if (expr -> left_expression -> IsConstant())
  6299.         {
  6300.             IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6301.             if (! left -> value)
  6302.                 expr -> value = control.int_pool.FindOrInsert(0);
  6303.             else if (expr -> right_expression -> IsConstant())
  6304.             {
  6305.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6306.                 expr -> value = control.int_pool.FindOrInsert(left -> value && right -> value ? 1 : 0);
  6307.             }
  6308.         }
  6309.     }
  6310.  
  6311.     expr -> symbol = control.boolean_type;
  6312.  
  6313.     return;
  6314. }
  6315.  
  6316.  
  6317. void Semantic::ProcessOR_OR(AstBinaryExpression *expr)
  6318. {
  6319.     ProcessExpression(expr -> left_expression);
  6320.     ProcessExpression(expr -> right_expression);
  6321.  
  6322.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6323.                *right_type = expr -> right_expression -> Type();
  6324.  
  6325.     if (left_type != control.no_type && right_type != control.no_type)
  6326.     {
  6327.         if (left_type != control.boolean_type)
  6328.         {
  6329.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6330.                            expr -> left_expression -> LeftToken(),
  6331.                            expr -> left_expression -> RightToken(),
  6332.                            left_type -> Name());
  6333.         }
  6334.         else if (right_type != control.boolean_type)
  6335.         {
  6336.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6337.                            expr -> right_expression -> LeftToken(),
  6338.                            expr -> right_expression -> RightToken(),
  6339.                            right_type -> Name());
  6340.         }
  6341.         else if (expr -> left_expression -> IsConstant())
  6342.         {
  6343.             IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6344.             if (left -> value)
  6345.                 expr -> value = control.int_pool.FindOrInsert(1);
  6346.             else if (expr -> right_expression -> IsConstant())
  6347.             {
  6348.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6349.                 expr -> value = control.int_pool.FindOrInsert(left -> value || right -> value ? 1 : 0);
  6350.             }
  6351.         }
  6352.     }
  6353.  
  6354.     expr -> symbol = control.boolean_type;
  6355.  
  6356.     return;
  6357. }
  6358.  
  6359.  
  6360. void Semantic::ProcessEQUAL_EQUAL(AstBinaryExpression *expr)
  6361. {
  6362.     ProcessExpressionOrStringConstant(expr -> left_expression);
  6363.     ProcessExpressionOrStringConstant(expr -> right_expression);
  6364.  
  6365.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6366.                *right_type = expr -> right_expression -> Type();
  6367.  
  6368.     if (left_type != control.no_type && right_type != control.no_type)
  6369.     {
  6370.         if (left_type != right_type)
  6371.         {
  6372.             if (left_type -> Primitive() && right_type -> Primitive())
  6373.                  BinaryNumericPromotion(expr);
  6374.             else if (CanCastConvert(left_type, right_type, expr -> binary_operator_token))
  6375.                  expr -> right_expression = ConvertToType(expr -> right_expression, left_type);
  6376.             else if (CanCastConvert(right_type, left_type, expr -> binary_operator_token))
  6377.                  expr -> left_expression = ConvertToType(expr -> left_expression, right_type);
  6378.             else
  6379.             {
  6380.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
  6381.                                expr -> LeftToken(),
  6382.                                expr -> RightToken(),
  6383.                                expr -> left_expression -> Type() -> ContainingPackage() -> PackageName(),
  6384.                                expr -> left_expression -> Type() -> ExternalName(),
  6385.                                expr -> right_expression -> Type() -> ContainingPackage() -> PackageName(),
  6386.                                expr -> right_expression -> Type() -> ExternalName());
  6387.             }
  6388.         }
  6389.         else
  6390.         {
  6391.             if (left_type == control.void_type)
  6392.                 ReportSemError(SemanticError::VOID_TYPE_IN_EQUALITY_EXPRESSION,
  6393.                                expr -> LeftToken(),
  6394.                                expr -> RightToken(),
  6395.                                expr -> left_expression -> Type() -> Name(),
  6396.                                expr -> right_expression -> Type() -> Name());
  6397.             expr -> symbol = left_type;
  6398.         }
  6399.  
  6400.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6401.         {
  6402.             LiteralValue *left = expr -> left_expression -> value;
  6403.             LiteralValue *right = expr -> right_expression -> value;
  6404.  
  6405.             if (expr -> Type() == control.double_type)
  6406.             {
  6407.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6408.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6409.  
  6410.                 expr -> value = control.int_pool.FindOrInsert(left -> value == right -> value ? 1 : 0);
  6411.             }
  6412.             else if (expr -> Type() == control.float_type)
  6413.             {
  6414.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6415.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6416.  
  6417.                 expr -> value = control.int_pool.FindOrInsert(left -> value == right -> value ? 1 : 0);
  6418.             }
  6419.             else expr -> value = control.int_pool.FindOrInsert(left == right ? 1 : 0);
  6420.         }
  6421.     }
  6422.  
  6423.     expr -> symbol = control.boolean_type;
  6424.  
  6425.     return;
  6426. }
  6427.  
  6428.  
  6429. void Semantic::ProcessNOT_EQUAL(AstBinaryExpression *expr)
  6430. {
  6431.     ProcessExpressionOrStringConstant(expr -> left_expression);
  6432.     ProcessExpressionOrStringConstant(expr -> right_expression);
  6433.  
  6434.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6435.                *right_type = expr -> right_expression -> Type();
  6436.  
  6437.     if (left_type != control.no_type && right_type != control.no_type)
  6438.     {
  6439.         if (left_type != right_type)
  6440.         {
  6441.             if (left_type -> Primitive() && right_type -> Primitive())
  6442.                  BinaryNumericPromotion(expr);
  6443.             else if (CanCastConvert(left_type, right_type, expr -> binary_operator_token))
  6444.                  expr -> right_expression = ConvertToType(expr -> right_expression, left_type);
  6445.             else if (CanCastConvert(right_type, left_type, expr -> binary_operator_token))
  6446.                  expr -> left_expression = ConvertToType(expr -> left_expression, right_type);
  6447.             else
  6448.             {
  6449.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
  6450.                                expr -> LeftToken(),
  6451.                                expr -> RightToken(),
  6452.                                expr -> left_expression -> Type() -> ContainingPackage() -> PackageName(),
  6453.                                expr -> left_expression -> Type() -> ExternalName(),
  6454.                                expr -> right_expression -> Type() -> ContainingPackage() -> PackageName(),
  6455.                                expr -> right_expression -> Type() -> ExternalName());
  6456.             }
  6457.         }
  6458.         else
  6459.         {
  6460.             if (left_type == control.void_type)
  6461.                 ReportSemError(SemanticError::VOID_TYPE_IN_EQUALITY_EXPRESSION,
  6462.                                expr -> LeftToken(),
  6463.                                expr -> RightToken());
  6464.             expr -> symbol = left_type;
  6465.         }
  6466.  
  6467.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6468.         {
  6469.             LiteralValue *left = expr -> left_expression -> value;
  6470.             LiteralValue *right = expr -> right_expression -> value;
  6471.  
  6472.             if (expr -> Type() == control.double_type)
  6473.             {
  6474.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6475.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6476.  
  6477.                 expr -> value = control.int_pool.FindOrInsert(left -> value != right -> value ? 1 : 0);
  6478.             }
  6479.             else if (expr -> Type() == control.float_type)
  6480.             {
  6481.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6482.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6483.  
  6484.                 expr -> value = control.int_pool.FindOrInsert(left -> value != right -> value ? 1 : 0);
  6485.             }
  6486.             else expr -> value = control.int_pool.FindOrInsert(left != right ? 1 : 0);
  6487.         }
  6488.     }
  6489.  
  6490.     expr -> symbol = control.boolean_type;
  6491.  
  6492.     return;
  6493. }
  6494.  
  6495.  
  6496. void Semantic::ProcessSTAR(AstBinaryExpression *expr)
  6497. {
  6498.     ProcessExpression(expr -> left_expression);
  6499.     ProcessExpression(expr -> right_expression);
  6500.  
  6501.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6502.                *right_type = expr -> right_expression -> Type();
  6503.  
  6504.     if (left_type == control.no_type || right_type == control.no_type)
  6505.         expr -> symbol = control.no_type;
  6506.     else
  6507.     {
  6508.         BinaryNumericPromotion(expr);
  6509.  
  6510.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6511.         {
  6512.             if (expr -> Type() == control.double_type)
  6513.             {
  6514.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6515.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6516.  
  6517.                 expr -> value = control.double_pool.FindOrInsert(left -> value * right -> value);
  6518.             }
  6519.             else if (expr -> Type() == control.float_type)
  6520.             {
  6521.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6522.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6523.  
  6524.                 expr -> value = control.float_pool.FindOrInsert(left -> value * right -> value);
  6525.             }
  6526.             else if (expr -> Type() == control.long_type)
  6527.             {
  6528.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6529.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6530.  
  6531.                 expr -> value = control.long_pool.FindOrInsert(left -> value * right -> value);
  6532.             }
  6533.             else if (expr -> Type() == control.int_type)
  6534.             {
  6535.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6536.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6537.  
  6538.                 expr -> value = control.int_pool.FindOrInsert(left -> value * right -> value);
  6539.             }
  6540.         }
  6541.     }
  6542.  
  6543.     return;
  6544. }
  6545.  
  6546.  
  6547. void Semantic::ProcessMINUS(AstBinaryExpression *expr)
  6548. {
  6549.     ProcessExpression(expr -> left_expression);
  6550.     ProcessExpression(expr -> right_expression);
  6551.  
  6552.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6553.                *right_type = expr -> right_expression -> Type();
  6554.  
  6555.     if (left_type == control.no_type || right_type == control.no_type)
  6556.         expr -> symbol = control.no_type;
  6557.     else
  6558.     {
  6559.         BinaryNumericPromotion(expr);
  6560.  
  6561.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6562.         {
  6563.             if (expr -> Type() == control.double_type)
  6564.             {
  6565.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6566.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6567.  
  6568.                 expr -> value = control.double_pool.FindOrInsert(left -> value - right -> value);
  6569.             }
  6570.             else if (expr -> Type() == control.float_type)
  6571.             {
  6572.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6573.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6574.  
  6575.                 expr -> value = control.float_pool.FindOrInsert(left -> value - right -> value);
  6576.             }
  6577.             else if (expr -> Type() == control.long_type)
  6578.             {
  6579.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6580.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6581.  
  6582.                 expr -> value = control.long_pool.FindOrInsert(left -> value - right -> value);
  6583.             }
  6584.             else if (expr -> Type() == control.int_type)
  6585.             {
  6586.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6587.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6588.  
  6589.                 expr -> value = control.int_pool.FindOrInsert(left -> value - right -> value);
  6590.             }
  6591.         }
  6592.     }
  6593.  
  6594.     return;
  6595. }
  6596.  
  6597.  
  6598. void Semantic::ProcessSLASH(AstBinaryExpression *expr)
  6599. {
  6600.     ProcessExpression(expr -> left_expression);
  6601.     ProcessExpression(expr -> right_expression);
  6602.  
  6603.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6604.                *right_type = expr -> right_expression -> Type();
  6605.  
  6606.     if (left_type == control.no_type || right_type == control.no_type)
  6607.         expr -> symbol = control.no_type;
  6608.     else
  6609.     {
  6610.         BinaryNumericPromotion(expr);
  6611.  
  6612.         //
  6613.         // This operation may throw ArithmeticException
  6614.         //
  6615.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  6616.         if (exception_set)
  6617.         {
  6618.             exception_set -> AddElement(control.RuntimeException());
  6619.             exception_set -> AddElement(control.Error());
  6620.         }
  6621.  
  6622.         AstExpression *left_expression = expr -> left_expression,
  6623.                       *right_expression = expr -> right_expression;
  6624.         if (right_expression -> IsConstant())
  6625.         {
  6626.             //
  6627.             // If the type of the expression is int or long and the right-hand side is 0
  6628.             // then issue an error message.
  6629.             // Otherwise, if both subexpressions are constant, calculate result.
  6630.             //
  6631.             if ((expr -> Type() == control.int_type && ((IntLiteralValue *) right_expression -> value) -> value == 0) ||
  6632.                 (expr -> Type() == control.long_type && ((LongLiteralValue *) right_expression -> value) -> value == 0))
  6633.             {
  6634.                 ReportSemError(SemanticError::ZERO_DIVIDE,
  6635.                                expr -> LeftToken(),
  6636.                                expr -> RightToken());
  6637.             }
  6638.             else if (left_expression -> IsConstant())
  6639.             {
  6640.                 if (expr -> Type() == control.double_type)
  6641.                 {
  6642.                     DoubleLiteralValue *left = (DoubleLiteralValue *) left_expression -> value;
  6643.                     DoubleLiteralValue *right = (DoubleLiteralValue *) right_expression -> value;
  6644.  
  6645.                     expr -> value = control.double_pool.FindOrInsert(left -> value / right -> value);
  6646.                 }
  6647.                 else if (expr -> Type() == control.float_type)
  6648.                 {
  6649.                     FloatLiteralValue *left = (FloatLiteralValue *) left_expression -> value;
  6650.                     FloatLiteralValue *right = (FloatLiteralValue *) right_expression -> value;
  6651.  
  6652.                     expr -> value = control.float_pool.FindOrInsert(left -> value / right -> value);
  6653.                 }
  6654.                 else if (expr -> Type() == control.long_type)
  6655.                 {
  6656.                     LongLiteralValue *left = (LongLiteralValue *) left_expression -> value;
  6657.                     LongLiteralValue *right = (LongLiteralValue *) right_expression -> value;
  6658.  
  6659.                     expr -> value = control.long_pool.FindOrInsert(left -> value / right -> value);
  6660.                 }
  6661.                 else if (expr -> Type() == control.int_type)
  6662.                 {
  6663.                     IntLiteralValue *left = (IntLiteralValue *) left_expression -> value;
  6664.                     IntLiteralValue *right = (IntLiteralValue *) right_expression -> value;
  6665.  
  6666.                     //
  6667.                     // There is a bug in the intel hardware where if one tries to compute ((2**32-1) / -1),
  6668.                     // he gets a ZeroDivide exception. Thus, instead of using the straightforward code below,
  6669.                     // we use the short-circuited one that follows:
  6670.                     //
  6671.                     //  expr -> value = control.int_pool.FindOrInsert(left -> value / right -> value);
  6672.                     //
  6673.                     expr -> value = control.int_pool.FindOrInsert(right -> value == -1 ? -(left -> value)
  6674.                                                                                        : left -> value / right -> value);
  6675.                 }
  6676.             }
  6677.         }
  6678.     }
  6679.  
  6680.     return;
  6681. }
  6682.  
  6683.  
  6684. void Semantic::ProcessMOD(AstBinaryExpression *expr)
  6685. {
  6686.     ProcessExpression(expr -> left_expression);
  6687.     ProcessExpression(expr -> right_expression);
  6688.  
  6689.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6690.                *right_type = expr -> right_expression -> Type();
  6691.  
  6692.     if (left_type == control.no_type || right_type == control.no_type)
  6693.         expr -> symbol = control.no_type;
  6694.     else
  6695.     {
  6696.         BinaryNumericPromotion(expr);
  6697.  
  6698.         //
  6699.         // This operation may throw ArithmeticException
  6700.         //
  6701.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  6702.         if (exception_set)
  6703.         {
  6704.             exception_set -> AddElement(control.RuntimeException());
  6705.             exception_set -> AddElement(control.Error());
  6706.         }
  6707.  
  6708.         AstExpression *left_expression = expr -> left_expression,
  6709.                       *right_expression = expr -> right_expression;
  6710.         if (right_expression -> IsConstant())
  6711.         {
  6712.             //
  6713.             // If the type of the expression is int or long and the right-hand side is 0
  6714.             // then issue an error message.
  6715.             // Otherwise, if both subexpressions are constant, calculate result.
  6716.             //
  6717.             if ((expr -> Type() == control.int_type && ((IntLiteralValue *) right_expression -> value) -> value == 0) ||
  6718.                 (expr -> Type() == control.long_type && ((LongLiteralValue *) right_expression -> value) -> value == 0))
  6719.             {
  6720.                 ReportSemError(SemanticError::ZERO_DIVIDE,
  6721.                                expr -> LeftToken(),
  6722.                                expr -> RightToken());
  6723.             }
  6724.             else if (left_expression -> IsConstant())
  6725.             {
  6726.                 if (expr -> Type() == control.double_type)
  6727.                 {
  6728.                     DoubleLiteralValue *left = (DoubleLiteralValue *) left_expression -> value;
  6729.                     DoubleLiteralValue *right = (DoubleLiteralValue *) right_expression -> value;
  6730.                     IEEEdouble result = IEEEdouble((u4) 0);
  6731.                     IEEEdouble::Fmodulus(left -> value, right -> value, result);
  6732.  
  6733.                     expr -> value = control.double_pool.FindOrInsert(result);
  6734.                 }
  6735.                 else if (expr -> Type() == control.float_type)
  6736.                 {
  6737.                     FloatLiteralValue *left = (FloatLiteralValue *) left_expression -> value;
  6738.                     FloatLiteralValue *right = (FloatLiteralValue *) right_expression -> value;
  6739.                     IEEEfloat result = IEEEfloat(0);
  6740.                     IEEEfloat::Fmodulus(left -> value, right -> value, result);
  6741.  
  6742.                     expr -> value = control.float_pool.FindOrInsert(result);
  6743.                 }
  6744.                 else if (expr -> Type() == control.long_type)
  6745.                 {
  6746.                     LongLiteralValue *left = (LongLiteralValue *) left_expression -> value;
  6747.                     LongLiteralValue *right = (LongLiteralValue *) right_expression -> value;
  6748.  
  6749.                     expr -> value = control.long_pool.FindOrInsert(left -> value % right -> value);
  6750.                 }
  6751.                 else if (expr -> Type() == control.int_type)
  6752.                 {
  6753.                     IntLiteralValue *left = (IntLiteralValue *) left_expression -> value;
  6754.                     IntLiteralValue *right = (IntLiteralValue *) right_expression -> value;
  6755.  
  6756.                     //
  6757.                     // There is a bug in the intel hardware where if one tries to compute ((2**32-1) / -1),
  6758.                     // he gets a ZeroDivide exception. Thus, instead of using the straightforward code below,
  6759.                     // we use the short-circuited one that follows:
  6760.                     //
  6761.                     // expr -> value = control.int_pool.FindOrInsert(left -> value % right -> value);
  6762.                     //
  6763.                     expr -> value = control.int_pool.FindOrInsert((left -> value  == (signed) 0x80000000 &&
  6764.                                                                    right -> value == (signed) 0xffffffff)
  6765.                                                                                    ? 0
  6766.                                                                                    : left -> value % right -> value);
  6767.                 }
  6768.             }
  6769.         }
  6770.     }
  6771.  
  6772.     return;
  6773. }
  6774.  
  6775.  
  6776. void Semantic::ProcessINSTANCEOF(AstBinaryExpression *expr)
  6777. {
  6778.     ProcessExpression(expr -> left_expression);
  6779.     ProcessExpression(expr -> right_expression);
  6780.  
  6781.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6782.                *right_type = expr -> right_expression -> Type();
  6783.  
  6784.     if (left_type -> Primitive())
  6785.     {
  6786.         ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  6787.                        expr -> left_expression -> LeftToken(),
  6788.                        expr -> left_expression -> RightToken(),
  6789.                        expr -> left_expression -> Type() -> Name());
  6790.     }
  6791.     else if (! CanCastConvert(right_type, left_type, expr -> binary_operator_token)) // can left_type (source) be cast into right_type
  6792.     {
  6793.         ReportSemError(SemanticError::INVALID_INSTANCEOF_CONVERSION,
  6794.                        expr -> LeftToken(),
  6795.                        expr -> RightToken(),
  6796.                        left_type -> ContainingPackage() -> PackageName(),
  6797.                        left_type -> ExternalName(),
  6798.                        right_type -> ContainingPackage() -> PackageName(),
  6799.                        right_type -> ExternalName());
  6800.     }
  6801.  
  6802.     expr -> symbol = control.boolean_type;
  6803.  
  6804.     return;
  6805. }
  6806.  
  6807.  
  6808. void Semantic::ProcessBinaryExpression(Ast *expr)
  6809. {
  6810.     AstBinaryExpression *binary_expression = (AstBinaryExpression *) expr;
  6811.     (this ->* ProcessBinaryExpr[binary_expression -> binary_tag])(binary_expression);
  6812.  
  6813.     return;
  6814. }
  6815.  
  6816.  
  6817. void Semantic::ProcessTypeExpression(Ast *expr)
  6818. {
  6819.     AstTypeExpression *type_expression = (AstTypeExpression *) expr;
  6820.  
  6821.     AstArrayType *array_type = type_expression -> type -> ArrayTypeCast();
  6822.     Ast *actual_type = (array_type ? array_type -> type : type_expression -> type);
  6823.  
  6824.     AstPrimitiveType *primitive_type = actual_type -> PrimitiveTypeCast();
  6825.     TypeSymbol *type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(actual_type));
  6826.  
  6827.     if (array_type)
  6828.         type = type -> GetArrayType((Semantic *) this, array_type -> NumBrackets());
  6829.  
  6830.     type_expression -> symbol = type;
  6831.  
  6832.     return;
  6833. }
  6834.  
  6835.  
  6836. void Semantic::ProcessConditionalExpression(Ast *expr)
  6837. {
  6838.     AstConditionalExpression *conditional_expression = (AstConditionalExpression *) expr;
  6839.  
  6840.     ProcessExpression(conditional_expression -> test_expression);
  6841.     ProcessExpressionOrStringConstant(conditional_expression -> true_expression);
  6842.     ProcessExpressionOrStringConstant(conditional_expression -> false_expression);
  6843.  
  6844.     TypeSymbol *test_type  = conditional_expression -> test_expression -> Type();
  6845.     TypeSymbol *true_type  = conditional_expression -> true_expression -> Type();
  6846.     TypeSymbol *false_type = conditional_expression -> false_expression -> Type();
  6847.  
  6848.     if (test_type == control.no_type || true_type == control.no_type || false_type == control.no_type)
  6849.         conditional_expression -> symbol = control.no_type;
  6850.     else if (test_type != control.boolean_type)
  6851.     {
  6852.         ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6853.                        conditional_expression -> test_expression -> LeftToken(),
  6854.                        conditional_expression -> test_expression -> RightToken(),
  6855.                        conditional_expression -> test_expression -> Type() -> Name());
  6856.         conditional_expression -> symbol = control.no_type;
  6857.     }
  6858.     else if (true_type == control.void_type)
  6859.     {
  6860.         ReportSemError(SemanticError::TYPE_IS_VOID,
  6861.                        conditional_expression -> true_expression -> LeftToken(),
  6862.                        conditional_expression -> true_expression -> RightToken(),
  6863.                        conditional_expression -> true_expression -> Type() -> Name());
  6864.         conditional_expression -> symbol = control.no_type;
  6865.     }
  6866.     else if (false_type == control.void_type)
  6867.     {
  6868.         ReportSemError(SemanticError::TYPE_IS_VOID,
  6869.                        conditional_expression -> false_expression -> LeftToken(),
  6870.                        conditional_expression -> false_expression -> RightToken(),
  6871.                        conditional_expression -> false_expression -> Type() -> Name());
  6872.         conditional_expression -> symbol = control.no_type;
  6873.     }
  6874.     else if (true_type -> Primitive())
  6875.     {
  6876.         if (! false_type -> Primitive() ||
  6877.             (true_type != false_type && (true_type == control.boolean_type || false_type == control.boolean_type)))
  6878.         {
  6879.             ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
  6880.                            conditional_expression -> false_expression -> LeftToken(),
  6881.                            conditional_expression -> false_expression -> RightToken(),
  6882.                            conditional_expression -> false_expression -> Type() -> ContainingPackage() -> PackageName(),
  6883.                            conditional_expression -> false_expression -> Type() -> ExternalName(),
  6884.                            conditional_expression -> true_expression -> Type() -> ContainingPackage() -> PackageName(),
  6885.                            conditional_expression -> true_expression -> Type() -> ExternalName());
  6886.             conditional_expression -> symbol = control.no_type;
  6887.         }
  6888.         else // must be a numeric type
  6889.         {
  6890.             if (true_type == false_type)
  6891.                 conditional_expression -> symbol = true_type;
  6892.             else // must be a numeric type
  6893.             {
  6894.                 if (true_type == control.byte_type && false_type == control.short_type)
  6895.                 {
  6896.                     conditional_expression -> true_expression =
  6897.                                 ConvertToType(conditional_expression -> true_expression, control.short_type);
  6898.                     conditional_expression -> symbol = control.short_type;
  6899.                 }
  6900.                 else if (true_type == control.short_type && false_type == control.byte_type)
  6901.                 {
  6902.                     conditional_expression -> false_expression =
  6903.                         ConvertToType(conditional_expression -> false_expression, control.short_type);
  6904.                     conditional_expression -> symbol = control.short_type;
  6905.                 }
  6906.                 else if (IsIntValueRepresentableInType(conditional_expression -> false_expression, true_type))
  6907.                 {
  6908.                     conditional_expression -> false_expression =
  6909.                         ConvertToType(conditional_expression -> false_expression, true_type);
  6910.                     conditional_expression -> symbol = true_type;
  6911.                 }
  6912.                 else if (IsIntValueRepresentableInType(conditional_expression -> true_expression, false_type))
  6913.                 {
  6914.                     conditional_expression -> true_expression =
  6915.                          ConvertToType(conditional_expression -> true_expression, false_type);
  6916.                     conditional_expression -> symbol = false_type;
  6917.                 }
  6918.                 else BinaryNumericPromotion(conditional_expression);
  6919.             }
  6920.  
  6921.             //
  6922.             // If all the relevant subexpressions are constants, compute the results and
  6923.             // set the value of the expression accordingly.
  6924.             //
  6925.             if (conditional_expression -> test_expression -> IsConstant())
  6926.             {
  6927.                 IntLiteralValue *test = (IntLiteralValue *) conditional_expression -> test_expression -> value;
  6928.  
  6929.                 if (test -> value && conditional_expression -> true_expression -> IsConstant())
  6930.                      conditional_expression -> value = conditional_expression -> true_expression -> value;
  6931.                 else if ((! test -> value) && conditional_expression -> false_expression -> IsConstant())
  6932.                      conditional_expression -> value = conditional_expression -> false_expression -> value;
  6933.             }
  6934.         }
  6935.     }
  6936.     else
  6937.     {
  6938.         if (true_type == false_type)
  6939.             conditional_expression -> symbol = true_type;
  6940.         else if (false_type -> Primitive())
  6941.         {
  6942.             ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
  6943.                            conditional_expression -> false_expression -> LeftToken(),
  6944.                            conditional_expression -> false_expression -> RightToken(),
  6945.                            conditional_expression -> false_expression -> Type() -> ContainingPackage() -> PackageName(),
  6946.                            conditional_expression -> false_expression -> Type() -> ExternalName(),
  6947.                            conditional_expression -> true_expression -> Type() -> ContainingPackage() -> PackageName(),
  6948.                            conditional_expression -> true_expression -> Type() -> ExternalName());
  6949.             conditional_expression -> symbol = control.no_type;
  6950.         }
  6951.         else if (true_type == control.null_type)
  6952.             conditional_expression -> symbol = false_type;
  6953.         else if (false_type == control.null_type)
  6954.             conditional_expression -> symbol = true_type;
  6955.         else if (CanAssignmentConvert(false_type, conditional_expression -> true_expression))
  6956.         {
  6957.             conditional_expression -> true_expression = ConvertToType(conditional_expression -> true_expression, false_type);
  6958.             conditional_expression -> symbol = false_type;
  6959.         }
  6960.         else if (CanAssignmentConvert(true_type, conditional_expression -> false_expression))
  6961.         {
  6962.             conditional_expression -> false_expression = ConvertToType(conditional_expression -> false_expression, true_type);
  6963.             conditional_expression -> symbol = true_type;
  6964.         }
  6965.         else
  6966.         {
  6967.             ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
  6968.                            conditional_expression -> false_expression -> LeftToken(),
  6969.                            conditional_expression -> false_expression -> RightToken(),
  6970.                            conditional_expression -> false_expression -> Type() -> ContainingPackage() -> PackageName(),
  6971.                            conditional_expression -> false_expression -> Type() -> ExternalName(),
  6972.                            conditional_expression -> true_expression -> Type() -> ContainingPackage() -> PackageName(),
  6973.                            conditional_expression -> true_expression -> Type() -> ExternalName());
  6974.             conditional_expression -> symbol = control.no_type;
  6975.         }
  6976.     }
  6977.  
  6978.     return;
  6979. }
  6980.  
  6981.  
  6982. void Semantic::ProcessAssignmentExpression(Ast *expr)
  6983. {
  6984.     AstAssignmentExpression *assignment_expression = (AstAssignmentExpression *) expr;
  6985.  
  6986.     AstExpression *left_hand_side = assignment_expression -> left_hand_side;
  6987.  
  6988.     ProcessExpression(left_hand_side);
  6989.     ProcessExpressionOrStringConstant(assignment_expression -> expression);
  6990.     TypeSymbol *left_type = left_hand_side -> Type(),
  6991.                *right_type = assignment_expression -> expression -> Type();
  6992.  
  6993.     assignment_expression -> symbol = left_type;
  6994.  
  6995.     if (left_type == control.no_type || right_type == control.no_type)
  6996.         return;
  6997.  
  6998.     if (left_hand_side -> ArrayAccessCast())
  6999.     {
  7000.         //
  7001.         // This operation may throw ArrayStoreException
  7002.         //
  7003.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  7004.         if (exception_set && (! left_type -> Primitive()))
  7005.         {
  7006.             exception_set -> AddElement(control.RuntimeException());
  7007.             exception_set -> AddElement(control.Error());
  7008.         }
  7009.     }
  7010.     else // the left-hand-side is a name
  7011.     {
  7012.         MethodSymbol *read_method = NULL;
  7013.         AstSimpleName *simple_name = left_hand_side -> SimpleNameCast();
  7014.         if (simple_name)
  7015.         {
  7016.             if (simple_name -> resolution_opt)
  7017.                read_method = simple_name -> resolution_opt -> symbol -> MethodCast();
  7018.         }
  7019.         else
  7020.         {
  7021.             AstFieldAccess *field_access = (AstFieldAccess *) left_hand_side;
  7022.             if (field_access -> resolution_opt)
  7023.                 read_method = field_access -> resolution_opt -> symbol -> MethodCast();
  7024.         }
  7025.  
  7026.         if (read_method)
  7027.         {
  7028.             VariableSymbol *symbol = (VariableSymbol *) read_method -> accessed_member;
  7029.             assignment_expression -> write_method = TypeSymbol::GetWriteAccessMethod(symbol);
  7030.         }
  7031.     }
  7032.  
  7033.     if (assignment_expression -> assignment_tag == AstAssignmentExpression::EQUAL)
  7034.     {
  7035.         if (left_type != right_type)
  7036.         {
  7037.             if (CanAssignmentConvert(left_type, assignment_expression -> expression))
  7038.                 assignment_expression -> expression = ConvertToType(assignment_expression -> expression, left_type);
  7039.             else if (assignment_expression -> expression -> IsConstant() &&
  7040.                      control.IsSimpleIntegerValueType(left_type) &&
  7041.                      control.IsSimpleIntegerValueType(assignment_expression -> expression -> Type()))
  7042.             {
  7043.                 if (left_type == control.byte_type)
  7044.                      ReportSemError(SemanticError::INVALID_BYTE_VALUE,
  7045.                                     assignment_expression -> expression -> LeftToken(),
  7046.                                     assignment_expression -> expression -> RightToken());
  7047.                 else if (left_type == control.short_type)
  7048.                      ReportSemError(SemanticError::INVALID_SHORT_VALUE,
  7049.                                     assignment_expression -> expression -> LeftToken(),
  7050.                                     assignment_expression -> expression -> RightToken());
  7051.                 else if (left_type == control.int_type)
  7052.                      ReportSemError(SemanticError::INVALID_INT_VALUE,
  7053.                                     assignment_expression -> expression -> LeftToken(),
  7054.                                     assignment_expression -> expression -> RightToken());
  7055.                 else // assert(left_type == control.char_type);
  7056.                      ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  7057.                                     assignment_expression -> expression -> LeftToken(),
  7058.                                     assignment_expression -> expression -> RightToken());
  7059.             }
  7060.             else
  7061.             {
  7062.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_ASSIGNMENT,
  7063.                                assignment_expression -> LeftToken(),
  7064.                                assignment_expression -> RightToken(),
  7065.                                left_type -> ContainingPackage() -> PackageName(),
  7066.                                left_type -> ExternalName(),
  7067.                                right_type -> ContainingPackage() -> PackageName(),
  7068.                                right_type -> ExternalName());
  7069.             }
  7070.         }
  7071.  
  7072.         return;
  7073.     }
  7074.  
  7075.     //
  7076.     // In the current spec, it is stated that the type of both the left-hand
  7077.     // and right-hand side of an "op=" assignment must be primitive. However,
  7078.     // the left-hand side may be of type String if the operator is "+=" and
  7079.     // that case, the right-hand side may also be of type String (or anything
  7080.     // else).
  7081.     //
  7082.     // TODO: CONFIRM THAT THERE WAS A MISTAKE IN THE SPEC.
  7083.     //
  7084.     if (left_type == control.String() && assignment_expression -> assignment_tag == AstAssignmentExpression::PLUS_EQUAL)
  7085.     {
  7086.         if (right_type != control.String())
  7087.         {
  7088.             if (right_type == control.void_type)
  7089.                 ReportSemError(SemanticError::VOID_TO_STRING,
  7090.                                assignment_expression -> expression -> LeftToken(),
  7091.                                assignment_expression -> expression -> RightToken());
  7092.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.String());
  7093.         }
  7094.  
  7095.         return;
  7096.     }
  7097.  
  7098.     if (! left_type -> Primitive())
  7099.     {
  7100.         ReportSemError(SemanticError::TYPE_NOT_PRIMITIVE,
  7101.                        left_hand_side -> LeftToken(),
  7102.                        left_hand_side -> RightToken(),
  7103.                        left_type -> Name());
  7104.         return;
  7105.     }
  7106.  
  7107.     if (! right_type -> Primitive())
  7108.     {
  7109.         ReportSemError(SemanticError::TYPE_NOT_PRIMITIVE,
  7110.                        assignment_expression -> expression -> LeftToken(),
  7111.                        assignment_expression -> expression -> RightToken(),
  7112.                        right_type -> Name());
  7113.         return;
  7114.     }
  7115.  
  7116.     switch(assignment_expression -> assignment_tag)
  7117.     {
  7118.         case AstAssignmentExpression::PLUS_EQUAL:
  7119.         case AstAssignmentExpression::STAR_EQUAL:
  7120.         case AstAssignmentExpression::MINUS_EQUAL:
  7121.             BinaryNumericPromotion(assignment_expression);
  7122.             break;
  7123.         case AstAssignmentExpression::SLASH_EQUAL:
  7124.         case AstAssignmentExpression::MOD_EQUAL:
  7125.             BinaryNumericPromotion(assignment_expression);
  7126.             {
  7127.                 AstExpression *right_expression = assignment_expression -> expression;
  7128.                 if (right_expression -> IsConstant())
  7129.                 {
  7130.                     //
  7131.                     // If the type of the expression is int or long and the right-hand side is 0
  7132.                     // then issue an error message.
  7133.                     //
  7134.                     if ((left_type == control.int_type && ((IntLiteralValue *) right_expression -> value) -> value == 0) ||
  7135.                         (left_type == control.long_type && ((LongLiteralValue *) right_expression -> value) -> value == 0))
  7136.                     {
  7137.                         ReportSemError(SemanticError::ZERO_DIVIDE,
  7138.                                        assignment_expression -> LeftToken(),
  7139.                                        assignment_expression -> RightToken());
  7140.                     }
  7141.                 }
  7142.             }
  7143.             break;
  7144.         case AstAssignmentExpression::LEFT_SHIFT_EQUAL:
  7145.         case AstAssignmentExpression::RIGHT_SHIFT_EQUAL:
  7146.         case AstAssignmentExpression::UNSIGNED_RIGHT_SHIFT_EQUAL:
  7147.              if (! control.IsIntegral(left_type))
  7148.              {
  7149.                  ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  7150.                                 left_hand_side -> LeftToken(),
  7151.                                 left_hand_side -> RightToken(),
  7152.                                 left_type -> Name());
  7153.              }
  7154.  
  7155.              if (! control.IsIntegral(right_type))
  7156.              {
  7157.                  ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  7158.                                 assignment_expression -> expression -> LeftToken(),
  7159.                                 assignment_expression -> expression -> RightToken(),
  7160.                                 right_type -> Name());
  7161.              }
  7162.  
  7163.              assignment_expression -> left_hand_side = PromoteUnaryNumericExpression(left_hand_side);
  7164.              assignment_expression -> expression = PromoteUnaryNumericExpression(assignment_expression -> expression);
  7165.              if (assignment_expression -> expression -> Type() == control.long_type)
  7166.                  assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.int_type);
  7167.              break;
  7168.         case AstAssignmentExpression::AND_EQUAL:
  7169.         case AstAssignmentExpression::XOR_EQUAL:
  7170.         case AstAssignmentExpression::IOR_EQUAL:
  7171.              if (left_type != control.boolean_type || right_type != control.boolean_type) // if anyont of the exprs is not boolean
  7172.                  BinaryNumericPromotion(assignment_expression);
  7173.              break;
  7174.         default:
  7175.             assert(false);
  7176.             break;
  7177.     }
  7178.  
  7179.     return;
  7180. }
  7181.